Intel SPMD Program Compiler  1.10.0
cbackend.cpp
Go to the documentation of this file.
1 //===-- CBackend.cpp - Library for converting LLVM code to C --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This library converts LLVM code to C code, compilable by GCC and other C
11 // compilers.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ispc.h"
16 #include "module.h"
17 
18 #include <math.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sstream>
22 
23 #ifndef _MSC_VER
24 #include <inttypes.h>
25 #define HAVE_PRINTF_A 1
26 #define ENABLE_CBE_PRINTF_A 1
27 #endif
28 
29 #ifndef PRIx64
30 #define PRIx64 "llx"
31 #endif
32 
33 #include "llvmutil.h"
34 
35 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
36  #include "llvm/Constants.h"
37  #include "llvm/DerivedTypes.h"
38  #include "llvm/CallingConv.h"
39  #include "llvm/Module.h"
40  #include "llvm/Instructions.h"
41  #include "llvm/Intrinsics.h"
42  #include "llvm/IntrinsicInst.h"
43  #include "llvm/InlineAsm.h"
44 #else // LLVM 3.3+
45  #include "llvm/IR/Constants.h"
46  #include "llvm/IR/DerivedTypes.h"
47  #include "llvm/IR/CallingConv.h"
48  #include "llvm/IR/Module.h"
49  #include "llvm/IR/Instructions.h"
50  #include "llvm/IR/Intrinsics.h"
51  #include "llvm/IR/IntrinsicInst.h"
52  #include "llvm/IR/InlineAsm.h"
53 #endif
54 #include "llvm/Pass.h"
55 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6 // <= 3.6
56  #include "llvm/PassManager.h"
57 #else // LLVM 3.7+
58  #include "llvm/IR/LegacyPassManager.h"
59 #endif
60 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
61  #include "llvm/TypeFinder.h"
62 #else // LLVM_3_3+
63  #include "llvm/IR/TypeFinder.h"
64 #endif
65 #include "llvm/ADT/StringExtras.h"
66 #include "llvm/ADT/SmallString.h"
67 #include "llvm/ADT/STLExtras.h"
68 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_4 // 3.2, 3.3, 3.4
69  #include "llvm/Support/InstIterator.h"
70 #else // 3.5+
71  #include "llvm/IR/InstIterator.h"
72 #endif
73 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_5
74  #include "llvm/Analysis/FindUsedTypes.h"
75 #endif
76 #include "llvm/Analysis/LoopInfo.h"
77 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5
78  #include "llvm/IR/Verifier.h"
79  #include <llvm/IR/IRPrintingPasses.h>
80  #include "llvm/IR/CallSite.h"
81  #include "llvm/IR/CFG.h"
82  #include "llvm/IR/GetElementPtrTypeIterator.h"
83  #include "llvm/Support/FileSystem.h"
84 #else
85  #include "llvm/Analysis/Verifier.h"
86  #include <llvm/Assembly/PrintModulePass.h>
87  #include "llvm/Support/CallSite.h"
88  #include "llvm/Support/CFG.h"
89  #include "llvm/Support/GetElementPtrTypeIterator.h"
90 #endif
91 #include "llvm/Analysis/ValueTracking.h"
92 #include "llvm/CodeGen/Passes.h"
93 #include "llvm/CodeGen/IntrinsicLowering.h"
94 //#include "llvm/Target/Mangler.h"
95 #include "llvm/Transforms/Scalar.h"
96 #if ISPC_LLVM_VERSION >= ISPC_LLVM_7_0
97  #include "llvm/Transforms/Utils.h"
98 #endif
99 #include "llvm/MC/MCAsmInfo.h"
100 #include "llvm/MC/MCContext.h"
101 #include "llvm/MC/MCInstrInfo.h"
102 #include "llvm/MC/MCObjectFileInfo.h"
103 #include "llvm/MC/MCRegisterInfo.h"
104 #include "llvm/MC/MCSubtargetInfo.h"
105 #include "llvm/MC/MCSymbol.h"
106 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2 // 3.2
107  #include "llvm/DataLayout.h"
108 #else // LLVM 3.3+
109  #include "llvm/IR/DataLayout.h"
110 #endif
111 #include "llvm/Support/ErrorHandling.h"
112 #include "llvm/Support/FormattedStream.h"
113 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2 // 3.2
114  #include "llvm/Support/InstVisitor.h"
115 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_3_4 // 3.3, 3.4
116  #include "llvm/InstVisitor.h"
117 #else // LLVM 3.5+
118  #include "llvm/IR/InstVisitor.h"
119 #endif
120 #include "llvm/Support/MathExtras.h"
121 #include "llvm/Support/TargetRegistry.h"
122 #include "llvm/Support/Host.h"
123 #include "llvm/Target/TargetMachine.h"
124 
125 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_4 // 3.2, 3.3, 3.4
126  #include "llvm/Config/config.h"
127 #endif
128 
129 #include <llvm/Transforms/IPO.h>
130 #include <llvm/Transforms/Utils/BasicBlockUtils.h>
131 #include <llvm/Support/ToolOutputFile.h>
132 #if ISPC_LLVM_VERSION > ISPC_LLVM_7_0
133  #include "llvm/IR/PatternMatch.h"
134 #endif
135 #include <algorithm>
136 // Some ms header decided to define setjmp as _setjmp, undo this for this file.
137 #ifdef _MSC_VER
138 #undef setjmp
139 #define snprintf _snprintf
140 #endif
141 ///////////////////////////////////////////////////////////////////////////////
142 // This part of code was in LLVM's ConstantsScanner.h,
143 // but it was removed in revision #232397
144 namespace constant_scanner {
145 class constant_iterator : public std::iterator<std::forward_iterator_tag,
146  const llvm::Constant, ptrdiff_t> {
147  llvm::const_inst_iterator InstI; // Method instruction iterator
148  unsigned OpIdx; // Operand index
149 
150  bool isAtConstant() const {
151  assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
152  "isAtConstant called with invalid arguments!");
153  return llvm::isa<llvm::Constant>(InstI->getOperand(OpIdx));
154  }
155 
156 public:
157  constant_iterator(const llvm::Function *F) : InstI(llvm::inst_begin(F)), OpIdx(0) {
158  // Advance to first constant... if we are not already at constant or end
159  if (InstI != llvm::inst_end(F) && // InstI is valid?
160  (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
161  operator++();
162  }
163 
164  constant_iterator(const llvm::Function *F, bool) // end ctor
165  : InstI(llvm::inst_end(F)),
166  OpIdx(0) {}
167 
168  bool operator==(const constant_iterator &x) const {
169  return OpIdx == x.OpIdx && InstI == x.InstI;
170  }
171  bool operator!=(const constant_iterator &x) const { return !(*this == x); }
172 
173  pointer operator*() const {
174  assert(isAtConstant() && "Dereferenced an iterator at the end!");
175  return llvm::cast<llvm::Constant>(InstI->getOperand(OpIdx));
176  }
177 
178  constant_iterator &operator++() { // Preincrement implementation
179  ++OpIdx;
180  do {
181  unsigned NumOperands = InstI->getNumOperands();
182  while (OpIdx < NumOperands && !isAtConstant()) {
183  ++OpIdx;
184  }
185 
186  if (OpIdx < NumOperands) return *this; // Found a constant!
187  ++InstI;
188  OpIdx = 0;
189  } while (!InstI.atEnd());
190 
191  return *this; // At the end of the method
192  }
193 };
194 
195 inline constant_iterator constant_begin(const llvm::Function *F) {
196  return constant_iterator(F);
197 }
198 
199 inline constant_iterator constant_end(const llvm::Function *F) {
200  return constant_iterator(F, true);
201 }
202 
203 }
204 
205 ///////////////////////////////////////////////////////////////////////////////
206 // FIXME:
207 namespace {
208  /// TypeFinder - Walk over a module, identifying all of the types that are
209  /// used by the module.
210  class TypeFinder {
211  // To avoid walking constant expressions multiple times and other IR
212  // objects, we keep several helper maps.
213  llvm::DenseSet<const llvm::Value*> VisitedConstants;
214 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
215  llvm::DenseSet<const llvm::Metadata*> VisitedMDNodes;
216 #endif
217  llvm::DenseSet<llvm::Type*> VisitedTypes;
218  std::vector<llvm::ArrayType*> &ArrayTypes;
219  std::vector<llvm::IntegerType*> &IntegerTypes;
220  std::vector<bool> &IsVolatile;
221  std::vector<int> &Alignment;
222  public:
223  TypeFinder(std::vector<llvm::ArrayType*> &t, std::vector<llvm::IntegerType*> &i,
224  std::vector<bool> &v, std::vector<int> &a)
225  : ArrayTypes(t), IntegerTypes(i) , IsVolatile(v), Alignment(a){}
226 
227  void run(const llvm::Module &M) {
228  // Get types from global variables.
229  for (llvm::Module::const_global_iterator I = M.global_begin(),
230  E = M.global_end(); I != E; ++I) {
231  incorporateType(I->getType());
232  if (I->hasInitializer())
233  incorporateValue(I->getInitializer());
234  }
235 
236  // Get types from aliases.
237  for (llvm::Module::const_alias_iterator I = M.alias_begin(),
238  E = M.alias_end(); I != E; ++I) {
239  incorporateType(I->getType());
240  if (const llvm::Value *Aliasee = I->getAliasee())
241  incorporateValue(Aliasee);
242  }
243 
244  llvm::SmallVector<std::pair<unsigned, llvm::MDNode*>, 4> MDForInst;
245 
246  // Get types from functions.
247  for (llvm::Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
248  incorporateType(FI->getType());
249 
250  for (llvm::Function::const_iterator BB = FI->begin(), E = FI->end();
251  BB != E;++BB)
252  for (llvm::BasicBlock::const_iterator II = BB->begin(),
253  E = BB->end(); II != E; ++II) {
254  const llvm::Instruction &I = *II;
255 
256  // Operands of SwitchInsts changed format after 3.1
257  // Seems like there ought to be better way to do what we
258  // want here. For now, punt on SwitchInsts.
259  if (llvm::isa<llvm::SwitchInst>(&I)) continue;
260 
261  // Incorporate the type of the instruction and all its operands.
262  incorporateType(I.getType());
263  if (llvm::isa<llvm::StoreInst>(&I))
264  if (llvm::IntegerType *ITy = llvm::dyn_cast<llvm::IntegerType>(I.getType())) {
265  IntegerTypes.push_back(ITy);
266  const llvm::StoreInst *St = llvm::dyn_cast<llvm::StoreInst>(&I);
267  IsVolatile.push_back(St->isVolatile());
268  Alignment.push_back(St->getAlignment());
269  }
270 
271  if (llvm::isa<llvm::LoadInst>(&I))
272  if (llvm::IntegerType *ITy = llvm::dyn_cast<llvm::IntegerType>(I.getType())) {
273  IntegerTypes.push_back(ITy);
274  const llvm::LoadInst *St = llvm::dyn_cast<llvm::LoadInst>(&I);
275  IsVolatile.push_back(St->isVolatile());
276  Alignment.push_back(St->getAlignment());
277  }
278 
279  for (llvm::User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
280  OI != OE; ++OI)
281  incorporateValue(*OI);
282 
283  // Incorporate types hiding in metadata.
284  I.getAllMetadataOtherThanDebugLoc(MDForInst);
285  for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
286  incorporateMDNode(MDForInst[i].second);
287 
288  MDForInst.clear();
289  }
290  }
291 
292  for (llvm::Module::const_named_metadata_iterator I = M.named_metadata_begin(),
293  E = M.named_metadata_end(); I != E; ++I) {
294 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
295  const llvm::NamedMDNode *NMD = I;
296 #else /* LLVM 3.8+ */
297  const llvm::NamedMDNode *NMD = &*I;
298 #endif
299  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
300  incorporateMDNode(NMD->getOperand(i));
301  }
302  }
303 
304  private:
305  void incorporateType(llvm::Type *Ty) {
306  // Check to see if we're already visited this type.
307  if (!VisitedTypes.insert(Ty).second)
308  return;
309 
310  if (llvm::ArrayType *ATy = llvm::dyn_cast<llvm::ArrayType>(Ty))
311  ArrayTypes.push_back(ATy);
312 
313  // Recursively walk all contained types.
314  for (llvm::Type::subtype_iterator I = Ty->subtype_begin(),
315  E = Ty->subtype_end(); I != E; ++I)
316  incorporateType(*I);
317  }
318 
319  /// incorporateValue - This method is used to walk operand lists finding
320  /// types hiding in constant expressions and other operands that won't be
321  /// walked in other ways. GlobalValues, basic blocks, instructions, and
322  /// inst operands are all explicitly enumerated.
323  void incorporateValue(const llvm::Value *V) {
324 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_5 // 3.2, 3.3, 3.4, 3.5
325  if (const llvm::MDNode *M = llvm::dyn_cast<llvm::MDNode>(V)) {
326  incorporateMDNode(M);
327  return;
328  }
329 #else /* LLVN 3.6+ */
330  if (const llvm::MetadataAsValue *MV = llvm::dyn_cast<llvm::MetadataAsValue>(V)) {
331  incorporateMDNode(MV->getMetadata());
332  return;
333  }
334 #endif
335  if (!llvm::isa<llvm::Constant>(V) || llvm::isa<llvm::GlobalValue>(V)) return;
336 
337  // Already visited?
338  if (!VisitedConstants.insert(V).second)
339  return;
340 
341  // Check this type.
342  incorporateType(V->getType());
343 
344  // Look in operands for types.
345  const llvm::User *U = llvm::cast<llvm::User>(V);
346  for (llvm::Constant::const_op_iterator I = U->op_begin(),
347  E = U->op_end(); I != E;++I)
348  incorporateValue(*I);
349  }
350 
351 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_5 // 3.2, 3.3, 3.4, 3.5
352  void incorporateMDNode(const llvm::MDNode *V) {
353 
354  // Already visited?
355  if (!VisitedConstants.insert(V).second)
356  return;
357 
358  // Look in operands for types.
359  for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i)
360  if (llvm::Value *Op = V->getOperand(i))
361  incorporateValue(Op);
362  }
363 #else // LLVM 3.6+
364  void incorporateMDNode(const llvm::Metadata *M) {
365 
366  // Already visited?
367  if (!VisitedMDNodes.insert(M).second)
368  return;
369 
370  if (const llvm::MDNode* N = llvm::dyn_cast<llvm::MDNode>(M)) {
371  // Look in operands for types.
372  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
373  if (const llvm::Metadata *O = N->getOperand(i))
374  incorporateMDNode(O);
375  } else if (llvm::isa<llvm::MDString>(M)) {
376  // Nothing to do with MDString.
377  } else if (const llvm::ValueAsMetadata* V = llvm::dyn_cast<llvm::ValueAsMetadata>(M)) {
378  incorporateValue(V->getValue());
379  } else {
380  // Some unknown Metadata subclass - has LLVM introduced something new?
381  llvm_unreachable("Unknown Metadata subclass");
382  }
383  }
384 #endif
385 
386  };
387 } // end anonymous namespace
388 
389 static void findUsedArrayAndLongIntTypes(const llvm::Module *m, std::vector<llvm::ArrayType*> &t,
390  std::vector<llvm::IntegerType*> &i, std::vector<bool> &IsVolatile,
391  std::vector<int> &Alignment) {
392  TypeFinder(t, i, IsVolatile, Alignment).run(*m);
393 }
394 
395 
396 static bool is_vec16_i64_ty(llvm::Type *Ty) {
397  llvm::VectorType *VTy = llvm::dyn_cast<llvm::VectorType>(Ty);
398  if ((VTy != NULL) && (VTy->getElementType()->isIntegerTy()) &&
399  VTy->getElementType()->getPrimitiveSizeInBits() == 64)
400  return true;
401  return false;
402 }
403 
404 namespace {
405  class CBEMCAsmInfo : public llvm::MCAsmInfo {
406  public:
407  CBEMCAsmInfo() {
408 
409 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_4 // 3.2, 3.3, 3.4
410  GlobalPrefix = "";
411 #endif
412  PrivateGlobalPrefix = "";
413  }
414  };
415 
416  /// CWriter - This class is the main chunk of code that converts an LLVM
417  /// module to a C translation unit.
418  class CWriter : public llvm::FunctionPass, public llvm::InstVisitor<CWriter> {
419  llvm::formatted_raw_ostream &Out;
420  llvm::IntrinsicLowering *IL;
421  //llvm::Mangler *Mang;
422  llvm::LoopInfo *LI;
423  const llvm::Module *TheModule;
424  const llvm::MCAsmInfo* TAsm;
425  const llvm::MCRegisterInfo *MRI;
426  const llvm::MCObjectFileInfo *MOFI;
427  llvm::MCContext *TCtx;
428 
429  // FIXME: it's ugly to have the name be "TD" here, but it saves us
430  // lots of ifdefs in the below since the new DataLayout and the old
431  // TargetData have generally similar interfaces...
432  const llvm::DataLayout* TD;
433 
434  std::map<const llvm::ConstantFP *, unsigned> FPConstantMap;
435  std::map<const llvm::ConstantDataVector *, unsigned> VectorConstantMap;
436  unsigned VectorConstantIndex;
437  std::set<llvm::Function*> intrinsicPrototypesAlreadyGenerated;
438  std::set<const llvm::Argument*> ByValParams;
439  unsigned FPCounter;
440  unsigned OpaqueCounter;
441  llvm::DenseMap<const llvm::Value*, unsigned> AnonValueNumbers;
442  unsigned NextAnonValueNumber;
443 
444  std::string includeName;
445  int vectorWidth;
446 
447  /// UnnamedStructIDs - This contains a unique ID for each struct that is
448  /// either anonymous or has no name.
449  llvm::DenseMap<llvm::StructType*, unsigned> UnnamedStructIDs;
450  llvm::DenseMap<llvm::ArrayType *, unsigned> ArrayIDs;
451 
452  public:
453  static char ID;
454  explicit CWriter(llvm::formatted_raw_ostream &o, const char *incname,
455  int vecwidth)
456  : FunctionPass(ID), Out(o), IL(0), /* Mang(0), */ LI(0),
457  TheModule(0), TAsm(0), MRI(0), MOFI(0), TCtx(0), TD(0),
458  OpaqueCounter(0), NextAnonValueNumber(0),
459  includeName(incname ? incname : "generic_defs.h"),
460  vectorWidth(vecwidth) {
461 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6 // <= 3.6
462  initializeLoopInfoPass(*llvm::PassRegistry::getPassRegistry());
463 #else // LLVM 3.7+
464  initializeLoopInfoWrapperPassPass(*llvm::PassRegistry::getPassRegistry());
465 #endif
466  FPCounter = 0;
467  VectorConstantIndex = 0;
468  }
469 
470 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9 // <= 3.9
471  virtual const char *getPassName() const { return "C backend"; }
472 #else // LLVM 4.0+
473  virtual llvm::StringRef getPassName() const { return "C backend"; }
474 #endif
475 
476  void getAnalysisUsage(llvm::AnalysisUsage &AU) const {
477 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6 // <= 3.6
478  AU.addRequired<llvm::LoopInfo>();
479 #else // LLVM 3.7+
480  AU.addRequired<llvm::LoopInfoWrapperPass>();
481 #endif
482  AU.setPreservesAll();
483  }
484 
485  virtual bool doInitialization(llvm::Module &M);
486 
487  bool runOnFunction(llvm::Function &F) {
488  // Do not codegen any 'available_externally' functions at all, they have
489  // definitions outside the translation unit.
490  if (F.hasAvailableExternallyLinkage())
491  return false;
492 
493 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6 // <= 3.6
494  LI = &getAnalysis<llvm::LoopInfo>();
495 #else // LLVM 3.7+
496  LI = &getAnalysis<llvm::LoopInfoWrapperPass>().getLoopInfo();
497 #endif
498 
499  // Get rid of intrinsics we can't handle.
500  lowerIntrinsics(F);
501 
502  // Output all floating point constants that cannot be printed accurately.
503  printFloatingPointConstants(F);
504 
505  // Output all vector constants so they can be accessed with single
506  // vector loads
507  printVectorConstants(F);
508 
509  printFunction(F);
510  return false;
511  }
512 
513  virtual bool doFinalization(llvm::Module &M) {
514  // Free memory...
515  delete IL;
516  delete TD;
517  //delete Mang;
518  delete TCtx;
519  delete TAsm;
520  delete MRI;
521  delete MOFI;
522  FPConstantMap.clear();
523  VectorConstantMap.clear();
524  ByValParams.clear();
525  intrinsicPrototypesAlreadyGenerated.clear();
526  UnnamedStructIDs.clear();
527  ArrayIDs.clear();
528  return false;
529  }
530 
531  llvm::raw_ostream &printType(llvm::raw_ostream &Out, llvm::Type *Ty,
532  bool isSigned = false,
533  const std::string &VariableName = "",
534  bool IgnoreName = false,
535 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
536  const llvm::AttrListPtr &PAL = llvm::AttrListPtr()
537 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
538  const llvm::AttributeSet &PAL = llvm::AttributeSet()
539 #else // LLVM 5.0+
540  const llvm::AttributeList &PAL = llvm::AttributeList()
541 #endif
542  );
543  llvm::raw_ostream &printSimpleType(llvm::raw_ostream &Out, llvm::Type *Ty,
544  bool isSigned,
545  const std::string &NameSoFar = "");
546 
547  void printStructReturnPointerFunctionType(llvm::raw_ostream &Out,
548 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
549  const llvm::AttrListPtr &PAL,
550 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
551  const llvm::AttributeSet &PAL,
552 #else // LLVM 5.0+
553  const llvm::AttributeList &PAL,
554 #endif
555  llvm::PointerType *Ty);
556 
557  std::string getStructName(llvm::StructType *ST);
558  std::string getArrayName(llvm::ArrayType *AT);
559 
560  /// writeOperandDeref - Print the result of dereferencing the specified
561  /// operand with '*'. This is equivalent to printing '*' then using
562  /// writeOperand, but avoids excess syntax in some cases.
563  void writeOperandDeref(llvm::Value *Operand) {
564  if (isAddressExposed(Operand)) {
565  // Already something with an address exposed.
566  writeOperandInternal(Operand);
567  } else {
568  Out << "*(";
569  writeOperand(Operand);
570  Out << ")";
571  }
572  }
573 
574  void writeOperand(llvm::Value *Operand, bool Static = false);
575  void writeInstComputationInline(llvm::Instruction &I);
576  void writeOperandInternal(llvm::Value *Operand, bool Static = false);
577  void writeOperandWithCast(llvm::Value* Operand, unsigned Opcode);
578  void writeOperandWithCast(llvm::Value* Operand, const llvm::ICmpInst &I);
579  bool writeInstructionCast(const llvm::Instruction &I);
580 
581  void writeMemoryAccess(llvm::Value *Operand, llvm::Type *OperandType,
582  bool IsVolatile, unsigned Alignment);
583 
584  private :
585  void lowerIntrinsics(llvm::Function &F);
586  /// Prints the definition of the intrinsic function F. Supports the
587  /// intrinsics which need to be explicitly defined in the CBackend.
588  void printIntrinsicDefinition(const llvm::Function &F, llvm::raw_ostream &Out);
589 
590  void printModuleTypes();
591  void printContainedStructs(llvm::Type *Ty, llvm::SmallPtrSet<llvm::Type *, 16> &);
592  void printContainedArrays(llvm::ArrayType *ATy, llvm::SmallPtrSet<llvm::Type *, 16> &);
593  void printFloatingPointConstants(llvm::Function &F);
594  void printFloatingPointConstants(const llvm::Constant *C);
595  void printVectorConstants(llvm::Function &F);
596  void printFunctionSignature(const llvm::Function *F, bool Prototype);
597 
598  void printFunction(llvm::Function &);
599  void printBasicBlock(llvm::BasicBlock *BB);
600  void printLoop(llvm::Loop *L);
601 
602  bool printCast(unsigned opcode, llvm::Type *SrcTy, llvm::Type *DstTy);
603  void printConstant(llvm::Constant *CPV, bool Static);
604  void printConstantWithCast(llvm::Constant *CPV, unsigned Opcode);
605  bool printConstExprCast(const llvm::ConstantExpr *CE, bool Static);
606  void printConstantArray(llvm::ConstantArray *CPA, bool Static);
607  void printConstantVector(llvm::ConstantVector *CV, bool Static);
608  void printConstantDataSequential(llvm::ConstantDataSequential *CDS, bool Static);
609 
610  /// isAddressExposed - Return true if the specified value's name needs to
611  /// have its address taken in order to get a C value of the correct type.
612  /// This happens for global variables, byval parameters, and direct allocas.
613  bool isAddressExposed(const llvm::Value *V) const {
614  if (const llvm::Argument *A = llvm::dyn_cast<llvm::Argument>(V))
615  return ByValParams.count(A);
616  return llvm::isa<llvm::GlobalVariable>(V) || isDirectAlloca(V);
617  }
618 
619  // isInlinableInst - Attempt to inline instructions into their uses to build
620  // trees as much as possible. To do this, we have to consistently decide
621  // what is acceptable to inline, so that variable declarations don't get
622  // printed and an extra copy of the expr is not emitted.
623  //
624  static bool isInlinableInst(const llvm::Instruction &I) {
625  // Always inline cmp instructions, even if they are shared by multiple
626  // expressions. GCC generates horrible code if we don't.
627  if (llvm::isa<llvm::CmpInst>(I) && llvm::isa<llvm::VectorType>(I.getType()) == false)
628  return true;
629 
630 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // 3.5+
631  // This instruction returns a struct on LLVM older than 3.4, and can not be inlined
632  if (llvm::isa<llvm::AtomicCmpXchgInst>(I))
633  return false;
634 #endif
635 
636  // Must be an expression, must be used exactly once. If it is dead, we
637  // emit it inline where it would go.
638  if (I.getType() == llvm::Type::getVoidTy(I.getContext()) || !I.hasOneUse() ||
639 #if ISPC_LLVM_VERSION > ISPC_LLVM_7_0 // 8.0+
640  I.isTerminator()
641 #else
642  llvm::isa<llvm::TerminatorInst>(I)
643 #endif
644  || llvm::isa<llvm::CallInst>(I) || llvm::isa<llvm::PHINode>(I) ||
645  llvm::isa<llvm::LoadInst>(I) || llvm::isa<llvm::VAArgInst>(I) || llvm::isa<llvm::InsertElementInst>(I) ||
646  llvm::isa<llvm::InsertValueInst>(I) || llvm::isa<llvm::ExtractValueInst>(I) || llvm::isa<llvm::SelectInst>(I))
647  // Don't inline a load across a store or other bad things!
648  return false;
649 
650  // Must not be used in inline asm, extractelement, or shufflevector.
651  if (I.hasOneUse()) {
652 
653 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // 3.5+
654  const llvm::Instruction &User = llvm::cast<llvm::Instruction>(*I.user_back());
655 #else
656  const llvm::Instruction &User = llvm::cast<llvm::Instruction>(*I.use_back());
657 #endif
658  if (isInlineAsm(User) || llvm::isa<llvm::ExtractElementInst>(User) ||
659  llvm::isa<llvm::ShuffleVectorInst>(User) || llvm::isa<llvm::AtomicRMWInst>(User) ||
660  llvm::isa<llvm::AtomicCmpXchgInst>(User))
661  return false;
662  }
663 
664  // Only inline instruction it if it's use is in the same BB as the inst.
665 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // 3.5+
666  return I.getParent() == llvm::cast<llvm::Instruction>(I.user_back())->getParent();
667 #else
668  return I.getParent() == llvm::cast<llvm::Instruction>(I.use_back())->getParent();
669 #endif
670  }
671 
672  // isDirectAlloca - Define fixed sized allocas in the entry block as direct
673  // variables which are accessed with the & operator. This causes GCC to
674  // generate significantly better code than to emit alloca calls directly.
675  //
676  static const llvm::AllocaInst *isDirectAlloca(const llvm::Value *V) {
677  const llvm::AllocaInst *AI = llvm::dyn_cast<llvm::AllocaInst>(V);
678  if (!AI) return 0;
679  if (AI->isArrayAllocation())
680  return 0; // FIXME: we can also inline fixed size array allocas!
681  if (AI->getParent() != &AI->getParent()->getParent()->getEntryBlock())
682  return 0;
683  return AI;
684  }
685 
686  // isInlineAsm - Check if the instruction is a call to an inline asm chunk.
687  static bool isInlineAsm(const llvm::Instruction& I) {
688  if (const llvm::CallInst *CI = llvm::dyn_cast<llvm::CallInst>(&I))
689  return llvm::isa<llvm::InlineAsm>(CI->getCalledValue());
690  return false;
691  }
692 
693  // Instruction visitation functions
694  friend class llvm::InstVisitor<CWriter>;
695 
696  void visitReturnInst(llvm::ReturnInst &I);
697  void visitBranchInst(llvm::BranchInst &I);
698  void visitSwitchInst(llvm::SwitchInst &I);
699  void visitIndirectBrInst(llvm::IndirectBrInst &I);
700  void visitInvokeInst(llvm::InvokeInst &I) {
701  llvm_unreachable("Lowerinvoke pass didn't work!");
702  }
703  void visitResumeInst(llvm::ResumeInst &I) {
704  llvm_unreachable("DwarfEHPrepare pass didn't work!");
705  }
706  void visitUnreachableInst(llvm::UnreachableInst &I);
707 
708  void visitPHINode(llvm::PHINode &I);
709  void visitBinaryOperator(llvm::Instruction &I);
710  void visitICmpInst(llvm::ICmpInst &I);
711  void visitFCmpInst(llvm::FCmpInst &I);
712 
713  void visitCastInst (llvm::CastInst &I);
714  void visitSelectInst(llvm::SelectInst &I);
715  void visitCallInst (llvm::CallInst &I);
716  void visitInlineAsm(llvm::CallInst &I);
717  bool visitBuiltinCall(llvm::CallInst &I, llvm::Intrinsic::ID ID, bool &WroteCallee);
718 
719  void visitAllocaInst(llvm::AllocaInst &I);
720  void visitLoadInst (llvm::LoadInst &I);
721  void visitStoreInst (llvm::StoreInst &I);
722  void visitGetElementPtrInst(llvm::GetElementPtrInst &I);
723  void visitVAArgInst (llvm::VAArgInst &I);
724 
725  void visitInsertElementInst(llvm::InsertElementInst &I);
726  void visitExtractElementInst(llvm::ExtractElementInst &I);
727  void visitShuffleVectorInst(llvm::ShuffleVectorInst &SVI);
728 
729  void visitInsertValueInst(llvm::InsertValueInst &I);
730  void visitExtractValueInst(llvm::ExtractValueInst &I);
731 
732  void visitAtomicRMWInst(llvm::AtomicRMWInst &I);
733  void visitAtomicCmpXchgInst(llvm::AtomicCmpXchgInst &I);
734 
735  void visitInstruction(llvm::Instruction &I) {
736 #ifndef NDEBUG
737  llvm::errs() << "C Writer does not know about " << I;
738 #endif
739  llvm_unreachable(0);
740  }
741 
742  void outputLValue(llvm::Instruction *I) {
743  Out << " " << GetValueName(I) << " = ";
744  }
745 
746  bool isGotoCodeNecessary(llvm::BasicBlock *From, llvm::BasicBlock *To);
747  void printPHICopiesForSuccessor(llvm::BasicBlock *CurBlock,
748  llvm::BasicBlock *Successor, unsigned Indent);
749  void printBranchToBlock(llvm::BasicBlock *CurBlock, llvm::BasicBlock *SuccBlock,
750  unsigned Indent);
751  void printGEPExpression(llvm::Value *Ptr, llvm::gep_type_iterator I,
752  llvm::gep_type_iterator E, bool Static);
753 
754  std::string GetValueName(const llvm::Value *Operand);
755  };
756 }
757 
758 char CWriter::ID = 0;
759 
760 
761 
762 static std::string CBEMangle(const std::string &S) {
763  std::string Result;
764 
765  for (unsigned i = 0, e = S.size(); i != e; ++i) {
766  if (i+1 != e && ((S[i] == '>' && S[i+1] == '>') ||
767  (S[i] == '<' && S[i+1] == '<'))) {
768  Result += '_';
769  Result += 'A'+(S[i]&15);
770  Result += 'A'+((S[i]>>4)&15);
771  Result += '_';
772  i++;
773  } else if (isalnum(S[i]) || S[i] == '_' || S[i] == '<' || S[i] == '>') {
774  Result += S[i];
775  } else {
776  Result += '_';
777  Result += 'A'+(S[i]&15);
778  Result += 'A'+((S[i]>>4)&15);
779  Result += '_';
780  }
781  }
782  return Result;
783 }
784 
785 std::string CWriter::getStructName(llvm::StructType *ST) {
786  if (!ST->isLiteral() && !ST->getName().empty())
787  return CBEMangle("l_"+ST->getName().str());
788 
789  return "l_unnamed_" + llvm::utostr(UnnamedStructIDs[ST]);
790 }
791 
792 std::string CWriter::getArrayName(llvm::ArrayType *AT) {
793  return "l_array_" + llvm::utostr(ArrayIDs[AT]);
794 }
795 
796 
797 /// printStructReturnPointerFunctionType - This is like printType for a struct
798 /// return type, except, instead of printing the type as void (*)(Struct*, ...)
799 /// print it as "Struct (*)(...)", for struct return functions.
800 void CWriter::printStructReturnPointerFunctionType(llvm::raw_ostream &Out,
801 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
802  const llvm::AttrListPtr &PAL,
803 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
804  const llvm::AttributeSet &PAL,
805 #else // LLVM 5.0+
806  const llvm::AttributeList &PAL,
807 #endif
808  llvm::PointerType *TheTy) {
809  llvm::FunctionType *FTy = llvm::cast<llvm::FunctionType>(TheTy->getElementType());
810  std::string tstr;
811  llvm::raw_string_ostream FunctionInnards(tstr);
812  FunctionInnards << " (*) (";
813  bool PrintedType = false;
814 
815  llvm::FunctionType::param_iterator I = FTy->param_begin(), E = FTy->param_end();
816  llvm::Type *RetTy = llvm::cast<llvm::PointerType>(*I)->getElementType();
817  unsigned Idx = 1;
818  for (++I, ++Idx; I != E; ++I, ++Idx) {
819  if (PrintedType)
820  FunctionInnards << ", ";
821  llvm::Type *ArgTy = *I;
822 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
823  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::ByVal)) {
824 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
825  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::ByVal)) {
826 #else // LLVM 5.0+
827  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::ByVal)) {
828 #endif
829  assert(ArgTy->isPointerTy());
830  ArgTy = llvm::cast<llvm::PointerType>(ArgTy)->getElementType();
831  }
832  printType(FunctionInnards, ArgTy,
833 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
834  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::SExt),
835 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
836  PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::SExt),
837 #else // LLVM 5.0+
838  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::SExt),
839 #endif
840  "");
841  PrintedType = true;
842  }
843  if (FTy->isVarArg()) {
844  if (!PrintedType)
845  FunctionInnards << " int"; //dummy argument for empty vararg functs
846  FunctionInnards << ", ...";
847  } else if (!PrintedType) {
848  FunctionInnards << "void";
849  }
850  FunctionInnards << ')';
851  printType(Out, RetTy,
852 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
853  PAL.getParamAttributes(0).hasAttribute(llvm::Attributes::SExt),
854 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
855  PAL.getParamAttributes(0).hasAttribute(llvm::AttributeSet::ReturnIndex, llvm::Attribute::SExt),
856 #else // LLVM 5.0+
857  PAL.getParamAttributes(0).hasAttribute(llvm::Attribute::SExt),
858 #endif
859  FunctionInnards.str());
860 }
861 
862 llvm::raw_ostream &
863 CWriter::printSimpleType(llvm::raw_ostream &Out, llvm::Type *Ty, bool isSigned,
864  const std::string &NameSoFar) {
865  assert((Ty->isFloatingPointTy() || Ty->isX86_MMXTy() || Ty->isIntegerTy() || Ty->isVectorTy() || Ty->isVoidTy()) &&
866  "Invalid type for printSimpleType");
867  switch (Ty->getTypeID()) {
868  case llvm::Type::VoidTyID: return Out << "void " << NameSoFar;
869  case llvm::Type::IntegerTyID: {
870  unsigned NumBits = llvm::cast<llvm::IntegerType>(Ty)->getBitWidth();
871  if (NumBits == 1)
872  return Out << "bool " << NameSoFar;
873  else if (NumBits <= 8)
874  return Out << (isSigned?"":"u") << "int8_t " << NameSoFar;
875  else if (NumBits <= 16)
876  return Out << (isSigned?"":"u") << "int16_t " << NameSoFar;
877  else if (NumBits <= 32)
878  return Out << (isSigned?"":"u") << "int32_t " << NameSoFar;
879  else if (NumBits <= 64)
880  return Out << (isSigned?"":"u") << "int64_t "<< NameSoFar;
881  else
882  return Out << "iN<" << NumBits << "> " << NameSoFar;
883  }
884  case llvm::Type::FloatTyID: return Out << "float " << NameSoFar;
885  case llvm::Type::DoubleTyID: return Out << "double " << NameSoFar;
886  // Lacking emulation of FP80 on PPC, etc., we assume whichever of these is
887  // present matches host 'long double'.
888  case llvm::Type::X86_FP80TyID:
889  case llvm::Type::PPC_FP128TyID:
890  case llvm::Type::FP128TyID: return Out << "long double " << NameSoFar;
891 
892  case llvm::Type::X86_MMXTyID:
893  return printSimpleType(Out, llvm::Type::getInt32Ty(Ty->getContext()), isSigned,
894  " __attribute__((vector_size(64))) " + NameSoFar);
895 
896  case llvm::Type::VectorTyID: {
897  llvm::VectorType *VTy = llvm::cast<llvm::VectorType>(Ty);
898 #if 1
899  const char *suffix = NULL;
900  const llvm::Type *eltTy = VTy->getElementType();
901  if (eltTy->isFloatTy())
902  suffix = "f";
903  else if (eltTy->isDoubleTy())
904  suffix = "d";
905  else {
906  assert(eltTy->isIntegerTy());
907  switch (eltTy->getPrimitiveSizeInBits()) {
908  case 1:
909  suffix = "i1";
910  break;
911  case 8:
912  suffix = "i8";
913  break;
914  case 16:
915  suffix = "i16";
916  break;
917  case 32:
918  suffix = "i32";
919  break;
920  case 64:
921  suffix = "i64";
922  break;
923  default:
924  suffix = "iN";
925  break;
926  }
927  }
928 
929  return Out << "__vec" << VTy->getNumElements() << "_" << suffix << " " <<
930  NameSoFar;
931 #else
932  return printSimpleType(Out, VTy->getElementType(), isSigned,
933  " __attribute__((vector_size(" +
934  utostr(TD->getTypeAllocSize(VTy)) + " ))) " + NameSoFar);
935 #endif
936  }
937 
938  default:
939 #ifndef NDEBUG
940  llvm::errs() << "Unknown primitive type: " << *Ty << "\n";
941 #endif
942  llvm_unreachable(0);
943  }
944 }
945 
946 // Pass the Type* and the variable name and this prints out the variable
947 // declaration.
948 //
949 llvm::raw_ostream &CWriter::printType(llvm::raw_ostream &Out, llvm::Type *Ty,
950  bool isSigned, const std::string &NameSoFar,
951  bool IgnoreName,
952 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
953  const llvm::AttrListPtr &PAL
954 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
955  const llvm::AttributeSet &PAL
956 #else // LLVM 5.0+
957  const llvm::AttributeList &PAL
958 #endif
959  ) {
960 
961  if (Ty->isFloatingPointTy() || Ty->isX86_MMXTy() || Ty->isIntegerTy() || Ty->isVectorTy() || Ty->isVoidTy()) {
962  printSimpleType(Out, Ty, isSigned, NameSoFar);
963  return Out;
964  }
965 
966  switch (Ty->getTypeID()) {
967  case llvm::Type::FunctionTyID: {
968  llvm::FunctionType *FTy = llvm::cast<llvm::FunctionType>(Ty);
969  std::string tstr;
970  llvm::raw_string_ostream FunctionInnards(tstr);
971  FunctionInnards << " (" << NameSoFar << ") (";
972  unsigned Idx = 1;
973  for (llvm::FunctionType::param_iterator I = FTy->param_begin(),
974  E = FTy->param_end(); I != E; ++I) {
975  llvm::Type *ArgTy = *I;
976 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
977  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::ByVal)) {
978 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
979  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::ByVal)) {
980 #else // LLVM 5.0+
981  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::ByVal)) {
982 #endif
983  assert(ArgTy->isPointerTy());
984  ArgTy = llvm::cast<llvm::PointerType>(ArgTy)->getElementType();
985  }
986  if (I != FTy->param_begin())
987  FunctionInnards << ", ";
988  printType(FunctionInnards, ArgTy,
989 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
990  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::SExt),
991 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
992  PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::SExt),
993 #else // LLVM 5.0+
994  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::SExt),
995 #endif
996  "");
997  ++Idx;
998  }
999  if (FTy->isVarArg()) {
1000  if (!FTy->getNumParams())
1001  FunctionInnards << " int"; //dummy argument for empty vaarg functs
1002  FunctionInnards << ", ...";
1003  } else if (!FTy->getNumParams()) {
1004  FunctionInnards << "void";
1005  }
1006  FunctionInnards << ')';
1007  printType(Out, FTy->getReturnType(),
1008 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
1009  PAL.getParamAttributes(0).hasAttribute(llvm::Attributes::SExt),
1010 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
1011  PAL.getParamAttributes(0).hasAttribute(llvm::AttributeSet::ReturnIndex, llvm::Attribute::SExt),
1012 #else // LLVM 5.0+
1013  PAL.getParamAttributes(0).hasAttribute(llvm::Attribute::SExt),
1014 #endif
1015  FunctionInnards.str());
1016  return Out;
1017  }
1018  case llvm::Type::StructTyID: {
1019  llvm::StructType *STy = llvm::cast<llvm::StructType>(Ty);
1020 
1021  // Check to see if the type is named.
1022  if (!IgnoreName)
1023  return Out << getStructName(STy) << ' ' << NameSoFar;
1024 
1025  Out << "struct " << NameSoFar << " {\n";
1026 
1027  // print initialization func
1028  if (STy->getNumElements() > 0) {
1029  Out << " static " << NameSoFar << " init(";
1030  unsigned Idx = 0;
1031  for (llvm::StructType::element_iterator I = STy->element_begin(),
1032  E = STy->element_end(); I != E; ++I, ++Idx) {
1033  char buf[64];
1034  sprintf(buf, "v%d", Idx);
1035  printType(Out, *I, false, buf);
1036  if (Idx + 1 < STy->getNumElements())
1037  Out << ", ";
1038  }
1039  Out << ") {\n";
1040  Out << " " << NameSoFar << " ret;\n";
1041  for (Idx = 0; Idx < STy->getNumElements(); ++Idx)
1042  Out << " ret.field" << Idx << " = v" << Idx << ";\n";
1043  Out << " return ret;\n";
1044  Out << " }\n";
1045  }
1046 
1047  unsigned Idx = 0;
1048  for (llvm::StructType::element_iterator I = STy->element_begin(),
1049  E = STy->element_end(); I != E; ++I) {
1050  Out << " ";
1051  printType(Out, *I, false, "field" + llvm::utostr(Idx++));
1052  Out << ";\n";
1053  }
1054  Out << '}';
1055  if (STy->isPacked())
1056  Out << " __attribute__ ((packed))";
1057  return Out;
1058  }
1059 
1060  case llvm::Type::PointerTyID: {
1061  llvm::PointerType *PTy = llvm::cast<llvm::PointerType>(Ty);
1062  std::string ptrName = "*" + NameSoFar;
1063 
1064  if (PTy->getElementType()->isArrayTy() ||
1065  PTy->getElementType()->isVectorTy())
1066  ptrName = "(" + ptrName + ")";
1067 
1068  if (!PAL.isEmpty())
1069  // Must be a function ptr cast!
1070  return printType(Out, PTy->getElementType(), false, ptrName, true, PAL);
1071  return printType(Out, PTy->getElementType(), false, ptrName);
1072  }
1073 
1074  case llvm::Type::ArrayTyID: {
1075  llvm::ArrayType *ATy = llvm::cast<llvm::ArrayType>(Ty);
1076 
1077  // Check to see if the type is named.
1078  if (!IgnoreName)
1079  return Out << getArrayName(ATy) << ' ' << NameSoFar;
1080 
1081  unsigned NumElements = (unsigned)ATy->getNumElements();
1082  if (NumElements == 0) NumElements = 1;
1083  // Arrays are wrapped in structs to allow them to have normal
1084  // value semantics (avoiding the array "decay").
1085  Out << "struct " << NameSoFar << " {\n";
1086  // init func
1087  Out << " static " << NameSoFar << " init(";
1088  for (unsigned Idx = 0; Idx < NumElements; ++Idx) {
1089  char buf[64];
1090  sprintf(buf, "v%d", Idx);
1091  printType(Out, ATy->getElementType(), false, buf);
1092  if (Idx + 1 < NumElements)
1093  Out << ", ";
1094  }
1095  Out << ") {\n";
1096  Out << " " << NameSoFar << " ret;\n";
1097  for (unsigned Idx = 0; Idx < NumElements; ++Idx)
1098  Out << " ret.array[" << Idx << "] = v" << Idx << ";\n";
1099  Out << " return ret;\n";
1100  Out << " }\n ";
1101 
1102  // if it's an array of i8s, also provide a version that takes a const
1103  // char *
1104  if (ATy->getElementType() == LLVMTypes::Int8Type) {
1105  Out << " static " << NameSoFar << " init(const char *p) {\n";
1106  Out << " " << NameSoFar << " ret;\n";
1107  Out << " memcpy((uint8_t *)ret.array, (uint8_t *)p, " << NumElements << ");\n";
1108  Out << " return ret;\n";
1109  Out << " }\n";
1110  }
1111 
1112  printType(Out, ATy->getElementType(), false,
1113  "array[" + llvm::utostr(NumElements) + "]");
1114  return Out << ";\n} ";
1115  }
1116 
1117  default:
1118  llvm_unreachable("Unhandled case in getTypeProps!");
1119  }
1120 }
1121 
1122 void CWriter::printConstantArray(llvm::ConstantArray *CPA, bool Static) {
1123  // vec16_i64 should be handled separately
1124 
1125  if (is_vec16_i64_ty(CPA->getOperand(0)->getType())) {
1126  Out << "/* vec16_i64 should be loaded carefully on knc */";
1127  Out << "\n#if defined(KNC)\n";
1128  Out << "hilo2zmm";
1129  Out << "\n#endif\n";
1130  }
1131  Out << "(";
1132  printConstant(llvm::cast<llvm::Constant>(CPA->getOperand(0)), Static);
1133  Out << ")";
1134 
1135  for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
1136  Out << ", ";
1137 
1138  if (is_vec16_i64_ty(CPA->getOperand(i)->getType())) {
1139  Out << "/* vec16_i64 should be loaded carefully on knc */";
1140  Out << "\n#if defined(KNC) \n";
1141  Out << "hilo2zmm";
1142  Out << "\n#endif \n";
1143  }
1144  Out << "(";
1145  printConstant(llvm::cast<llvm::Constant>(CPA->getOperand(i)), Static);
1146  Out << ")";
1147  }
1148 }
1149 
1150 void CWriter::printConstantVector(llvm::ConstantVector *CP, bool Static) {
1151  printConstant(llvm::cast<llvm::Constant>(CP->getOperand(0)), Static);
1152  for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
1153  Out << ", ";
1154  printConstant(llvm::cast<llvm::Constant>(CP->getOperand(i)), Static);
1155  }
1156 }
1157 
1158 void CWriter::printConstantDataSequential(llvm::ConstantDataSequential *CDS,
1159  bool Static) {
1160  // As a special case, print the array as a string if it is an array of
1161  // ubytes or an array of sbytes with positive values.
1162  //
1163  if (CDS->isCString()) {
1164  Out << '\"';
1165  // Keep track of whether the last number was a hexadecimal escape.
1166  bool LastWasHex = false;
1167 
1168  llvm::StringRef Bytes = CDS->getAsCString();
1169 
1170  // Do not include the last character, which we know is null
1171  for (unsigned i = 0, e = Bytes.size(); i != e; ++i) {
1172  unsigned char C = Bytes[i];
1173 
1174  // Print it out literally if it is a printable character. The only thing
1175  // to be careful about is when the last letter output was a hex escape
1176  // code, in which case we have to be careful not to print out hex digits
1177  // explicitly (the C compiler thinks it is a continuation of the previous
1178  // character, sheesh...)
1179  //
1180  if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
1181  LastWasHex = false;
1182  if (C == '"' || C == '\\')
1183  Out << "\\" << (char)C;
1184  else
1185  Out << (char)C;
1186  } else {
1187  LastWasHex = false;
1188  switch (C) {
1189  case '\n': Out << "\\n"; break;
1190  case '\t': Out << "\\t"; break;
1191  case '\r': Out << "\\r"; break;
1192  case '\v': Out << "\\v"; break;
1193  case '\a': Out << "\\a"; break;
1194  case '\"': Out << "\\\""; break;
1195  case '\'': Out << "\\\'"; break;
1196  default:
1197  Out << "\\x";
1198  Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
1199  Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
1200  LastWasHex = true;
1201  break;
1202  }
1203  }
1204  }
1205  Out << '\"';
1206  } else {
1207  printConstant(CDS->getElementAsConstant(0), Static);
1208  for (unsigned i = 1, e = CDS->getNumElements(); i != e; ++i) {
1209  Out << ", ";
1210  printConstant(CDS->getElementAsConstant(i), Static);
1211  }
1212  }
1213 }
1214 
1215 static inline std::string ftostr(const llvm::APFloat& V) {
1216  std::string Buf;
1217 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9
1218  if (&V.getSemantics() == &llvm::APFloat::IEEEdouble) {
1219  llvm::raw_string_ostream(Buf) << V.convertToDouble();
1220  return Buf;
1221  } else if (&V.getSemantics() == &llvm::APFloat::IEEEsingle) {
1222  llvm::raw_string_ostream(Buf) << (double)V.convertToFloat();
1223  return Buf;
1224  }
1225 #else // LLVM 4.0+
1226  if (&V.getSemantics() == &llvm::APFloat::IEEEdouble()) {
1227  llvm::raw_string_ostream(Buf) << V.convertToDouble();
1228  return Buf;
1229  } else if (&V.getSemantics() == &llvm::APFloat::IEEEsingle()) {
1230  llvm::raw_string_ostream(Buf) << (double)V.convertToFloat();
1231  return Buf;
1232  }
1233 #endif
1234  return "<unknown format in ftostr>"; // error
1235 }
1236 
1237 // isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
1238 // textually as a double (rather than as a reference to a stack-allocated
1239 // variable). We decide this by converting CFP to a string and back into a
1240 // double, and then checking whether the conversion results in a bit-equal
1241 // double to the original value of CFP. This depends on us and the target C
1242 // compiler agreeing on the conversion process (which is pretty likely since we
1243 // only deal in IEEE FP).
1244 //
1245 static bool isFPCSafeToPrint(const llvm::ConstantFP *CFP) {
1246  bool ignored;
1247  // Do long doubles in hex for now.
1248  if (CFP->getType() != llvm::Type::getFloatTy(CFP->getContext()) &&
1249  CFP->getType() != llvm::Type::getDoubleTy(CFP->getContext()))
1250  return false;
1251  llvm::APFloat APF = llvm::APFloat(CFP->getValueAPF()); // copy
1252  if (CFP->getType() == llvm::Type::getFloatTy(CFP->getContext()))
1253 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9 // <= 3.9
1254  APF.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, &ignored);
1255 #else // LLVM 4.0+
1256  APF.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven, &ignored);
1257 #endif
1258 #if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
1259  char Buffer[100];
1260  sprintf(Buffer, "%a", APF.convertToDouble());
1261  if (!strncmp(Buffer, "0x", 2) ||
1262  !strncmp(Buffer, "-0x", 3) ||
1263  !strncmp(Buffer, "+0x", 3))
1264  return APF.bitwiseIsEqual(llvm::APFloat(atof(Buffer)));
1265  return false;
1266 #else
1267  std::string StrVal = ftostr(APF);
1268 
1269  while (StrVal[0] == ' ')
1270  StrVal.erase(StrVal.begin());
1271 
1272  // Check to make sure that the stringized number is not some string like "Inf"
1273  // or NaN. Check that the string matches the "[-+]?[0-9]" regex.
1274  if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1275  ((StrVal[0] == '-' || StrVal[0] == '+') &&
1276  (StrVal[1] >= '0' && StrVal[1] <= '9')))
1277  // Reparse stringized version!
1278  return APF.bitwiseIsEqual(llvm::APFloat(atof(StrVal.c_str())));
1279  return false;
1280 #endif
1281 }
1282 
1283 /// Print out the casting for a cast operation. This does the double casting
1284 /// necessary for conversion to the destination type, if necessary.
1285 /// Return value indicates whether a closing paren is needed.
1286 /// @brief Print a cast
1287 bool CWriter::printCast(unsigned opc, llvm::Type *SrcTy, llvm::Type *DstTy) {
1288  if (llvm::isa<const llvm::VectorType>(DstTy)) {
1289  assert(llvm::isa<const llvm::VectorType>(SrcTy));
1290  switch (opc) {
1291  case llvm::Instruction::UIToFP: Out << "__cast_uitofp("; break;
1292  case llvm::Instruction::SIToFP: Out << "__cast_sitofp("; break;
1293  case llvm::Instruction::IntToPtr: llvm_unreachable("Invalid vector cast");
1294  case llvm::Instruction::Trunc: Out << "__cast_trunc("; break;
1295  case llvm::Instruction::BitCast: Out << "__cast_bits("; break;
1296  case llvm::Instruction::FPExt: Out << "__cast_fpext("; break;
1297  case llvm::Instruction::FPTrunc: Out << "__cast_fptrunc("; break;
1298  case llvm::Instruction::ZExt: Out << "__cast_zext("; break;
1299  case llvm::Instruction::PtrToInt: llvm_unreachable("Invalid vector cast");
1300  case llvm::Instruction::FPToUI: Out << "__cast_fptoui("; break;
1301  case llvm::Instruction::SExt: Out << "__cast_sext("; break;
1302  case llvm::Instruction::FPToSI: Out << "__cast_fptosi("; break;
1303  default:
1304  llvm_unreachable("Invalid cast opcode");
1305  }
1306 
1307  // print a call to the constructor for the destination type for the
1308  // first arg; this bogus first parameter is only used to convey the
1309  // desired return type to the callee.
1310  printType(Out, DstTy);
1311  Out << "(), ";
1312 
1313  return true;
1314  }
1315 
1316  // Print the destination type cast
1317  switch (opc) {
1318  case llvm::Instruction::BitCast: {
1319  if (DstTy->isPointerTy()) {
1320  Out << '(';
1321  printType(Out, DstTy);
1322  Out << ')';
1323  break;
1324  }
1325  else {
1326  Out << "__cast_bits((";
1327  printType(Out, DstTy);
1328  Out << ")0, ";
1329  return true;
1330  }
1331  }
1332  case llvm::Instruction::UIToFP:
1333  case llvm::Instruction::SIToFP:
1334  case llvm::Instruction::IntToPtr:
1335  case llvm::Instruction::Trunc:
1336  case llvm::Instruction::FPExt:
1337  case llvm::Instruction::FPTrunc: // For these the DstTy sign doesn't matter
1338  Out << '(';
1339  printType(Out, DstTy);
1340  Out << ')';
1341  break;
1342  case llvm::Instruction::ZExt:
1343  case llvm::Instruction::PtrToInt:
1344  case llvm::Instruction::FPToUI: // For these, make sure we get an unsigned dest
1345  Out << '(';
1346  printSimpleType(Out, DstTy, false);
1347  Out << ')';
1348  break;
1349  case llvm::Instruction::SExt:
1350  case llvm::Instruction::FPToSI: // For these, make sure we get a signed dest
1351  Out << '(';
1352  printSimpleType(Out, DstTy, true);
1353  Out << ')';
1354  break;
1355  default:
1356  llvm_unreachable("Invalid cast opcode");
1357  }
1358 
1359  // Print the source type cast
1360  switch (opc) {
1361  case llvm::Instruction::UIToFP:
1362  case llvm::Instruction::ZExt:
1363  Out << '(';
1364  printSimpleType(Out, SrcTy, false);
1365  Out << ')';
1366  break;
1367  case llvm::Instruction::SIToFP:
1368  case llvm::Instruction::SExt:
1369  Out << '(';
1370  printSimpleType(Out, SrcTy, true);
1371  Out << ')';
1372  break;
1373  case llvm::Instruction::IntToPtr:
1374  case llvm::Instruction::PtrToInt:
1375  // Avoid "cast to pointer from integer of different size" warnings
1376  Out << "(unsigned long)";
1377  break;
1378  case llvm::Instruction::Trunc:
1379  case llvm::Instruction::BitCast:
1380  case llvm::Instruction::FPExt:
1381  case llvm::Instruction::FPTrunc:
1382  case llvm::Instruction::FPToSI:
1383  case llvm::Instruction::FPToUI:
1384  break; // These don't need a source cast.
1385  default:
1386  llvm_unreachable("Invalid cast opcode");
1387  break;
1388  }
1389  return false;
1390 }
1391 
1392 
1393 /** Construct the name of a function with the given base and returning a
1394  vector of a given type, of the specified idth. For example, if base
1395  is "foo" and matchType is i32 and width is 16, this will return the
1396  string "__foo_i32<__vec16_i32>".
1397  */
1398 static const char *
1399 lGetTypedFunc(const char *base, llvm::Type *matchType, int width) {
1400  static const char *ty_desc_str[] = {"f", "d", "i1", "i8", "i16", "i32", "i64"};
1401  static const char *fn_desc_str[] = {"float", "double", "i1", "i8", "i16", "i32", "i64"};
1402  enum {DESC_FLOAT, DESC_DOUBLE, DESC_I1, DESC_I8, DESC_I16, DESC_I32, DESC_I64} desc;
1403 
1404  switch (matchType->getTypeID()) {
1405  case llvm::Type::FloatTyID: desc = DESC_FLOAT; break;
1406  case llvm::Type::DoubleTyID: desc = DESC_DOUBLE; break;
1407  case llvm::Type::IntegerTyID: {
1408  switch (llvm::cast<llvm::IntegerType>(matchType)->getBitWidth()) {
1409  case 1: desc = DESC_I1; break;
1410  case 8: desc = DESC_I8; break;
1411  case 16: desc = DESC_I16; break;
1412  case 32: desc = DESC_I32; break;
1413  case 64: desc = DESC_I64; break;
1414  default: return NULL;
1415  }
1416  break;
1417  }
1418  default: return NULL;
1419  }
1420 
1421  char buf[64];
1422  snprintf(buf, 64, "__%s_%s<__vec%d_%s>", base, fn_desc_str[desc], width, ty_desc_str[desc]);
1423  return strdup(buf);
1424 }
1425 
1426 
1427 // printConstant - The LLVM Constant to C Constant converter.
1428 void CWriter::printConstant(llvm::Constant *CPV, bool Static) {
1429  if (const llvm::ConstantExpr *CE = llvm::dyn_cast<llvm::ConstantExpr>(CPV)) {
1430  if (llvm::isa<llvm::VectorType>(CPV->getType())) {
1431  assert(CE->getOpcode() == llvm::Instruction::BitCast);
1432  llvm::ConstantExpr *Op = llvm::dyn_cast<llvm::ConstantExpr>(CE->getOperand(0));
1433  assert(Op && Op->getOpcode() == llvm::Instruction::BitCast);
1434  assert(llvm::isa<llvm::VectorType>(Op->getOperand(0)->getType()));
1435 
1436  Out << "(__cast_bits(";
1437  printType(Out, CE->getType());
1438  Out << "(), ";
1439  printConstant(Op->getOperand(0), Static);
1440  Out << "))";
1441  return;
1442  }
1443  switch (CE->getOpcode()) {
1444  case llvm::Instruction::Trunc:
1445  case llvm::Instruction::ZExt:
1446  case llvm::Instruction::SExt:
1447  case llvm::Instruction::FPTrunc:
1448  case llvm::Instruction::FPExt:
1449  case llvm::Instruction::UIToFP:
1450  case llvm::Instruction::SIToFP:
1451  case llvm::Instruction::FPToUI:
1452  case llvm::Instruction::FPToSI:
1453  case llvm::Instruction::PtrToInt:
1454  case llvm::Instruction::IntToPtr:
1455  case llvm::Instruction::BitCast: {
1456  if (CE->getOpcode() == llvm::Instruction::BitCast &&
1457  CE->getType()->isPointerTy() == false) {
1458  Out << "__cast_bits((";
1459  printType(Out, CE->getType());
1460  Out << ")0, ";
1461  printConstant(CE->getOperand(0), Static);
1462  Out << ")";
1463  return;
1464  }
1465 
1466  Out << "(";
1467  bool closeParen = printCast(CE->getOpcode(), CE->getOperand(0)->getType(),
1468  CE->getType());
1469  if (CE->getOpcode() == llvm::Instruction::SExt &&
1470  CE->getOperand(0)->getType() == llvm::Type::getInt1Ty(CPV->getContext())) {
1471  // Make sure we really sext from bool here by subtracting from 0
1472  Out << "0-";
1473  }
1474  printConstant(CE->getOperand(0), Static);
1475  if (CE->getType() == llvm::Type::getInt1Ty(CPV->getContext()) &&
1476  (CE->getOpcode() == llvm::Instruction::Trunc ||
1477  CE->getOpcode() == llvm::Instruction::FPToUI ||
1478  CE->getOpcode() == llvm::Instruction::FPToSI ||
1479  CE->getOpcode() == llvm::Instruction::PtrToInt)) {
1480  // Make sure we really truncate to bool here by anding with 1
1481  Out << "&1u";
1482  }
1483  Out << ')';
1484  if (closeParen)
1485  Out << ')';
1486  return;
1487  }
1488  case llvm::Instruction::GetElementPtr:
1489  assert(!llvm::isa<llvm::VectorType>(CPV->getType()));
1490  Out << "(";
1491  printGEPExpression(CE->getOperand(0), gep_type_begin(CPV),
1492  gep_type_end(CPV), Static);
1493  Out << ")";
1494  return;
1495  case llvm::Instruction::Select:
1496  assert(!llvm::isa<llvm::VectorType>(CPV->getType()));
1497  Out << '(';
1498  printConstant(CE->getOperand(0), Static);
1499  Out << '?';
1500  printConstant(CE->getOperand(1), Static);
1501  Out << ':';
1502  printConstant(CE->getOperand(2), Static);
1503  Out << ')';
1504  return;
1505  case llvm::Instruction::Add:
1506  case llvm::Instruction::FAdd:
1507  case llvm::Instruction::Sub:
1508  case llvm::Instruction::FSub:
1509  case llvm::Instruction::Mul:
1510  case llvm::Instruction::FMul:
1511  case llvm::Instruction::SDiv:
1512  case llvm::Instruction::UDiv:
1513  case llvm::Instruction::FDiv:
1514  case llvm::Instruction::URem:
1515  case llvm::Instruction::SRem:
1516  case llvm::Instruction::FRem:
1517  case llvm::Instruction::And:
1518  case llvm::Instruction::Or:
1519  case llvm::Instruction::Xor:
1520  case llvm::Instruction::ICmp:
1521  case llvm::Instruction::Shl:
1522  case llvm::Instruction::LShr:
1523  case llvm::Instruction::AShr:
1524  {
1525  assert(!llvm::isa<llvm::VectorType>(CPV->getType()));
1526  Out << '(';
1527  bool NeedsClosingParens = printConstExprCast(CE, Static);
1528  printConstantWithCast(CE->getOperand(0), CE->getOpcode());
1529  switch (CE->getOpcode()) {
1530  case llvm::Instruction::Add:
1531  case llvm::Instruction::FAdd: Out << " + "; break;
1532  case llvm::Instruction::Sub:
1533  case llvm::Instruction::FSub: Out << " - "; break;
1534  case llvm::Instruction::Mul:
1535  case llvm::Instruction::FMul: Out << " * "; break;
1536  case llvm::Instruction::URem:
1537  case llvm::Instruction::SRem:
1538  case llvm::Instruction::FRem: Out << " % "; break;
1539  case llvm::Instruction::UDiv:
1540  case llvm::Instruction::SDiv:
1541  case llvm::Instruction::FDiv: Out << " / "; break;
1542  case llvm::Instruction::And: Out << " & "; break;
1543  case llvm::Instruction::Or: Out << " | "; break;
1544  case llvm::Instruction::Xor: Out << " ^ "; break;
1545  case llvm::Instruction::Shl: Out << " << "; break;
1546  case llvm::Instruction::LShr:
1547  case llvm::Instruction::AShr: Out << " >> "; break;
1548  case llvm::Instruction::ICmp:
1549  switch (CE->getPredicate()) {
1550  case llvm::ICmpInst::ICMP_EQ: Out << " == "; break;
1551  case llvm::ICmpInst::ICMP_NE: Out << " != "; break;
1552  case llvm::ICmpInst::ICMP_SLT:
1553  case llvm::ICmpInst::ICMP_ULT: Out << " < "; break;
1554  case llvm::ICmpInst::ICMP_SLE:
1555  case llvm::ICmpInst::ICMP_ULE: Out << " <= "; break;
1556  case llvm::ICmpInst::ICMP_SGT:
1557  case llvm::ICmpInst::ICMP_UGT: Out << " > "; break;
1558  case llvm::ICmpInst::ICMP_SGE:
1559  case llvm::ICmpInst::ICMP_UGE: Out << " >= "; break;
1560  default: llvm_unreachable("Illegal ICmp predicate");
1561  }
1562  break;
1563  default: llvm_unreachable("Illegal opcode here!");
1564  }
1565  printConstantWithCast(CE->getOperand(1), CE->getOpcode());
1566  if (NeedsClosingParens)
1567  Out << "))";
1568  Out << ')';
1569  return;
1570  }
1571  case llvm::Instruction::FCmp: {
1572  assert(!llvm::isa<llvm::VectorType>(CPV->getType()));
1573  Out << '(';
1574  bool NeedsClosingParens = printConstExprCast(CE, Static);
1575  if (CE->getPredicate() == llvm::FCmpInst::FCMP_FALSE)
1576  Out << "0";
1577  else if (CE->getPredicate() == llvm::FCmpInst::FCMP_TRUE)
1578  Out << "1";
1579  else {
1580  const char* op = 0;
1581  switch (CE->getPredicate()) {
1582  default: llvm_unreachable("Illegal FCmp predicate");
1583  case llvm::FCmpInst::FCMP_ORD: op = "ord"; break;
1584  case llvm::FCmpInst::FCMP_UNO: op = "uno"; break;
1585  case llvm::FCmpInst::FCMP_UEQ: op = "ueq"; break;
1586  case llvm::FCmpInst::FCMP_UNE: op = "une"; break;
1587  case llvm::FCmpInst::FCMP_ULT: op = "ult"; break;
1588  case llvm::FCmpInst::FCMP_ULE: op = "ule"; break;
1589  case llvm::FCmpInst::FCMP_UGT: op = "ugt"; break;
1590  case llvm::FCmpInst::FCMP_UGE: op = "uge"; break;
1591  case llvm::FCmpInst::FCMP_OEQ: op = "oeq"; break;
1592  case llvm::FCmpInst::FCMP_ONE: op = "one"; break;
1593  case llvm::FCmpInst::FCMP_OLT: op = "olt"; break;
1594  case llvm::FCmpInst::FCMP_OLE: op = "ole"; break;
1595  case llvm::FCmpInst::FCMP_OGT: op = "ogt"; break;
1596  case llvm::FCmpInst::FCMP_OGE: op = "oge"; break;
1597  }
1598  Out << "llvm_fcmp_" << op << "(";
1599  printConstantWithCast(CE->getOperand(0), CE->getOpcode());
1600  Out << ", ";
1601  printConstantWithCast(CE->getOperand(1), CE->getOpcode());
1602  Out << ")";
1603  }
1604  if (NeedsClosingParens)
1605  Out << "))";
1606  Out << ')';
1607  return;
1608  }
1609  default:
1610 #ifndef NDEBUG
1611  llvm::errs() << "CWriter Error: Unhandled constant expression: "
1612  << *CE << "\n";
1613 #endif
1614  llvm_unreachable(0);
1615  }
1616  } else if (llvm::isa<llvm::UndefValue>(CPV) && CPV->getType()->isSingleValueType()) {
1617  if (CPV->getType()->isVectorTy()) {
1618  printType(Out, CPV->getType());
1619  Out << "( /* UNDEF */)";
1620  return;
1621  }
1622 
1623  Out << "((";
1624  printType(Out, CPV->getType()); // sign doesn't matter
1625  Out << ")/*UNDEF*/";
1626  Out << "0)";
1627  return;
1628  }
1629 
1630  if (llvm::ConstantInt *CI = llvm::dyn_cast<llvm::ConstantInt>(CPV)) {
1631  llvm::Type* Ty = CI->getType();
1632  if (Ty == llvm::Type::getInt1Ty(CPV->getContext()))
1633  Out << (CI->getZExtValue() ? '1' : '0');
1634  else if (Ty == llvm::Type::getInt32Ty(CPV->getContext()))
1635  Out << CI->getZExtValue() << 'u';
1636  else if (Ty == llvm::Type::getInt64Ty(CPV->getContext()))
1637  Out << CI->getZExtValue() << "ull";
1638  else if (Ty->getPrimitiveSizeInBits() > 64) {
1639  Out << "\"";
1640  //const uint64_t *Ptr64 = CPV->getUniqueInteger().getRawData();
1641  const uint64_t *Ptr64 = CI->getValue().getRawData();
1642  for (unsigned i = 0; i < Ty->getPrimitiveSizeInBits(); i++) {
1643  Out << ((Ptr64[i / (sizeof (uint64_t) * 8)] >> (i % (sizeof (uint64_t) * 8))) & 1);
1644  }
1645  Out << "\"";
1646  }
1647  else {
1648  Out << "((";
1649  printSimpleType(Out, Ty, false) << ')';
1650  if (CI->isMinValue(true))
1651  Out << CI->getZExtValue() << 'u';
1652  else
1653  Out << CI->getSExtValue();
1654  Out << ')';
1655  }
1656  return;
1657  }
1658 
1659  switch (CPV->getType()->getTypeID()) {
1660  case llvm::Type::FloatTyID:
1661  case llvm::Type::DoubleTyID:
1662  case llvm::Type::X86_FP80TyID:
1663  case llvm::Type::PPC_FP128TyID:
1664  case llvm::Type::FP128TyID: {
1665  llvm::ConstantFP *FPC = llvm::cast<llvm::ConstantFP>(CPV);
1666  std::map<const llvm::ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
1667  if (I != FPConstantMap.end()) {
1668  // Because of FP precision problems we must load from a stack allocated
1669  // value that holds the value in hex.
1670  Out << "(*(" << (FPC->getType() == llvm::Type::getFloatTy(CPV->getContext()) ?
1671  "float" :
1672  FPC->getType() == llvm::Type::getDoubleTy(CPV->getContext()) ?
1673  "double" :
1674  "long double")
1675  << "*)&FPConstant" << I->second << ')';
1676  } else {
1677  double V;
1678  if (FPC->getType() == llvm::Type::getFloatTy(CPV->getContext()))
1679  V = FPC->getValueAPF().convertToFloat();
1680  else if (FPC->getType() == llvm::Type::getDoubleTy(CPV->getContext()))
1681  V = FPC->getValueAPF().convertToDouble();
1682  else {
1683  // Long double. Convert the number to double, discarding precision.
1684  // This is not awesome, but it at least makes the CBE output somewhat
1685  // useful.
1686  llvm::APFloat Tmp = FPC->getValueAPF();
1687  bool LosesInfo;
1688 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9 // <= 3.9
1689  Tmp.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmTowardZero, &LosesInfo);
1690 #else // LLVM 4.0+
1691  Tmp.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmTowardZero, &LosesInfo);
1692 #endif
1693  V = Tmp.convertToDouble();
1694  }
1695 
1696  if (std::isnan(V)) {
1697  // The value is NaN
1698 
1699  // FIXME the actual NaN bits should be emitted.
1700  // The prefix for a quiet NaN is 0x7FF8. For a signalling NaN,
1701  // it's 0x7ff4.
1702  const unsigned long QuietNaN = 0x7ff8UL;
1703  //const unsigned long SignalNaN = 0x7ff4UL;
1704 
1705  // We need to grab the first part of the FP #
1706  char Buffer[100];
1707 
1708  uint64_t ll = llvm::DoubleToBits(V);
1709  sprintf(Buffer, "0x%" PRIx64, ll);
1710 
1711  std::string Num(&Buffer[0], &Buffer[6]);
1712  unsigned long Val = strtoul(Num.c_str(), 0, 16);
1713 
1714  if (FPC->getType() == llvm::Type::getFloatTy(FPC->getContext()))
1715  Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "F(\""
1716  << Buffer << "\") /*nan*/ ";
1717  else
1718  Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "(\""
1719  << Buffer << "\") /*nan*/ ";
1720  } else if (std::isinf(V)) {
1721  // The value is Inf
1722  if (V < 0) Out << '-';
1723  Out << "LLVM_INF" <<
1724  (FPC->getType() == llvm::Type::getFloatTy(FPC->getContext()) ? "F" : "")
1725  << " /*inf*/ ";
1726  } else {
1727  std::string Num;
1728 #if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
1729  // Print out the constant as a floating point number.
1730  char Buffer[100];
1731  sprintf(Buffer, "%a", V);
1732  Num = Buffer;
1733 #else
1734  Num = ftostr(FPC->getValueAPF());
1735 #endif
1736  Out << Num;
1737  }
1738  }
1739  break;
1740  }
1741 
1742  case llvm::Type::ArrayTyID: {
1743  llvm::ArrayType *AT = llvm::cast<llvm::ArrayType>(CPV->getType());
1744  if (Static)
1745  // arrays are wrapped in structs...
1746  Out << "{ ";
1747  else {
1748  // call init func of the struct it's wrapped in...
1749  printType(Out, CPV->getType());
1750  Out << "::init (";
1751  }
1752  if (llvm::ConstantArray *CA = llvm::dyn_cast<llvm::ConstantArray>(CPV)) {
1753  printConstantArray(CA, Static);
1754  } else if (llvm::ConstantDataSequential *CDS =
1755  llvm::dyn_cast<llvm::ConstantDataSequential>(CPV)) {
1756  printConstantDataSequential(CDS, Static);
1757  } else {
1758  assert(llvm::isa<llvm::ConstantAggregateZero>(CPV) || llvm::isa<llvm::UndefValue>(CPV));
1759  if (AT->getNumElements()) {
1760  Out << ' ';
1761  llvm::Constant *CZ = llvm::Constant::getNullValue(AT->getElementType());
1762  printConstant(CZ, Static);
1763  for (unsigned i = 1, e = (unsigned)AT->getNumElements(); i != e; ++i) {
1764  Out << ", ";
1765  printConstant(CZ, Static);
1766  }
1767  }
1768  }
1769  if (Static)
1770  Out << " }";
1771  else
1772  Out << ")";
1773  break;
1774  }
1775  case llvm::Type::VectorTyID: {
1776  llvm::VectorType *VT = llvm::dyn_cast<llvm::VectorType>(CPV->getType());
1777 
1778  if (llvm::isa<llvm::ConstantAggregateZero>(CPV)) {
1779  // All zeros; call the __setzero_* function.
1780  const char *setZeroFunc = lGetTypedFunc("setzero", VT->getElementType(), vectorWidth);
1781  assert(setZeroFunc != NULL);
1782  Out << setZeroFunc << "()";
1783  }
1784  else if (llvm::isa<llvm::UndefValue>(CPV)) {
1785  // Undefined value; call __undef_* so that we can potentially pass
1786  // this information along..
1787  const char *undefFunc = lGetTypedFunc("undef", VT->getElementType(), vectorWidth);
1788  assert(undefFunc != NULL);
1789  Out << undefFunc << "()";
1790  }
1791  else {
1792  const char *smearFunc = lGetTypedFunc("smear", VT->getElementType(), vectorWidth);
1793 
1794  if (llvm::ConstantVector *CV = llvm::dyn_cast<llvm::ConstantVector>(CPV)) {
1795  llvm::Constant *splatValue = CV->getSplatValue();
1796  if (splatValue != NULL && smearFunc != NULL) {
1797  // If it's a basic type and has a __smear_* function, then
1798  // call that.
1799  Out << smearFunc << "(";
1800  printConstant(splatValue, Static);
1801  Out << ")";
1802  }
1803  else {
1804  // Otherwise call the constructor for the type
1805  printType(Out, CPV->getType());
1806  Out << "(";
1807  printConstantVector(CV, Static);
1808  Out << ")";
1809  }
1810  }
1811  else if (llvm::ConstantDataVector *CDV =
1812  llvm::dyn_cast<llvm::ConstantDataVector>(CPV)) {
1813  llvm::Constant *splatValue = CDV->getSplatValue();
1814  if (splatValue != NULL && smearFunc != NULL) {
1815  Out << smearFunc << "(";
1816  printConstant(splatValue, Static);
1817  Out << ")";
1818  }
1819  else if (VectorConstantMap.find(CDV) != VectorConstantMap.end()) {
1820  // If we have emitted an static const array with the
1821  // vector's values, just load from it.
1822  unsigned index = VectorConstantMap[CDV];
1823  int alignment = 4 * std::min(vectorWidth, 16);
1824 
1825  Out << "__load<" << alignment << ">(";
1826 
1827  // Cast the pointer to the array of element values to a
1828  // pointer to the vector type.
1829  Out << "(const ";
1830  printSimpleType(Out, CDV->getType(), true, "");
1831  Out << " *)";
1832 
1833  Out << "(VectorConstant" << index << "))";
1834  }
1835  else {
1836  printType(Out, CPV->getType());
1837  Out << "(";
1838  printConstantDataSequential(CDV, Static);
1839  Out << ")";
1840  }
1841  }
1842  else {
1843  llvm::report_fatal_error("Unexpected vector type");
1844  }
1845  }
1846 
1847  break;
1848  }
1849  case llvm::Type::StructTyID:
1850  if (!Static) {
1851  // call init func...
1852  printType(Out, CPV->getType());
1853  Out << "::init";
1854  }
1855  if (llvm::isa<llvm::ConstantAggregateZero>(CPV) || llvm::isa<llvm::UndefValue>(CPV)) {
1856  llvm::StructType *ST = llvm::cast<llvm::StructType>(CPV->getType());
1857  Out << '(';
1858  if (ST->getNumElements()) {
1859  Out << ' ';
1860  printConstant(llvm::Constant::getNullValue(ST->getElementType(0)), Static);
1861  for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
1862  Out << ", ";
1863  printConstant(llvm::Constant::getNullValue(ST->getElementType(i)), Static);
1864  }
1865  }
1866  Out << ')';
1867  } else {
1868  Out << '(';
1869  if (CPV->getNumOperands()) {
1870  // It is a kludge. It is needed because we cannot support short vectors
1871  // when generating code for knl-generic in multitarget mode.
1872  // Short vectors are mapped to "native" vectors and cause AVX-512 code
1873  // generation in static block initialization (__vec16_* in ::init function).
1874  bool isGenericKNL = g->target->getISA() == Target::GENERIC &&
1875  !g->target->getTreatGenericAsSmth().empty() &&
1877  if (isGenericKNL && CPV->getOperand(0)->getType()->isVectorTy())
1878  llvm::report_fatal_error("knl-generic-* target doesn's support short vectors");
1879  Out << ' ';
1880  printConstant(llvm::cast<llvm::Constant>(CPV->getOperand(0)), Static);
1881  for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
1882  Out << ", ";
1883  if (isGenericKNL && CPV->getOperand(i)->getType()->isVectorTy())
1884  llvm::report_fatal_error("knl-generic-* target doesn's support short vectors");
1885  printConstant(llvm::cast<llvm::Constant>(CPV->getOperand(i)), Static);
1886  }
1887  }
1888  Out << ')';
1889  }
1890  break;
1891 
1892  case llvm::Type::PointerTyID:
1893  if (llvm::isa<llvm::ConstantPointerNull>(CPV)) {
1894  Out << "((";
1895  printType(Out, CPV->getType()); // sign doesn't matter
1896  Out << ")/*NULL*/0)";
1897  break;
1898  } else if (llvm::GlobalValue *GV = llvm::dyn_cast<llvm::GlobalValue>(CPV)) {
1899  writeOperand(GV, Static);
1900  break;
1901  }
1902  // FALL THROUGH
1903  default:
1904 #ifndef NDEBUG
1905  llvm::errs() << "Unknown constant type: " << *CPV << "\n";
1906 #endif
1907  llvm_unreachable(0);
1908  }
1909 }
1910 
1911 // Some constant expressions need to be casted back to the original types
1912 // because their operands were casted to the expected type. This function takes
1913 // care of detecting that case and printing the cast for the ConstantExpr.
1914 bool CWriter::printConstExprCast(const llvm::ConstantExpr* CE, bool Static) {
1915  bool NeedsExplicitCast = false;
1916  llvm::Type *Ty = CE->getOperand(0)->getType();
1917  bool TypeIsSigned = false;
1918  switch (CE->getOpcode()) {
1919  case llvm::Instruction::Add:
1920  case llvm::Instruction::Sub:
1921  case llvm::Instruction::Mul:
1922  // We need to cast integer arithmetic so that it is always performed
1923  // as unsigned, to avoid undefined behavior on overflow.
1924  case llvm::Instruction::LShr:
1925  case llvm::Instruction::URem:
1926  case llvm::Instruction::UDiv: NeedsExplicitCast = true; break;
1927  case llvm::Instruction::AShr:
1928  case llvm::Instruction::SRem:
1929  case llvm::Instruction::SDiv: NeedsExplicitCast = true; TypeIsSigned = true; break;
1930  case llvm::Instruction::SExt:
1931  Ty = CE->getType();
1932  NeedsExplicitCast = true;
1933  TypeIsSigned = true;
1934  break;
1935  case llvm::Instruction::ZExt:
1936  case llvm::Instruction::Trunc:
1937  case llvm::Instruction::FPTrunc:
1938  case llvm::Instruction::FPExt:
1939  case llvm::Instruction::UIToFP:
1940  case llvm::Instruction::SIToFP:
1941  case llvm::Instruction::FPToUI:
1942  case llvm::Instruction::FPToSI:
1943  case llvm::Instruction::PtrToInt:
1944  case llvm::Instruction::IntToPtr:
1945  case llvm::Instruction::BitCast:
1946  Ty = CE->getType();
1947  NeedsExplicitCast = true;
1948  break;
1949  default: break;
1950  }
1951  if (NeedsExplicitCast) {
1952  Out << "((";
1953  if (Ty->isIntegerTy() && Ty != llvm::Type::getInt1Ty(Ty->getContext()))
1954  printSimpleType(Out, Ty, TypeIsSigned);
1955  else
1956  printType(Out, Ty); // not integer, sign doesn't matter
1957  Out << ")(";
1958  }
1959  return NeedsExplicitCast;
1960 }
1961 
1962 // Print a constant assuming that it is the operand for a given Opcode. The
1963 // opcodes that care about sign need to cast their operands to the expected
1964 // type before the operation proceeds. This function does the casting.
1965 void CWriter::printConstantWithCast(llvm::Constant* CPV, unsigned Opcode) {
1966 
1967  // Extract the operand's type, we'll need it.
1968  llvm::Type* OpTy = CPV->getType();
1969 
1970  // Indicate whether to do the cast or not.
1971  bool shouldCast = false;
1972  bool typeIsSigned = false;
1973 
1974  // Based on the Opcode for which this Constant is being written, determine
1975  // the new type to which the operand should be casted by setting the value
1976  // of OpTy. If we change OpTy, also set shouldCast to true so it gets
1977  // casted below.
1978  switch (Opcode) {
1979  default:
1980  // for most instructions, it doesn't matter
1981  break;
1982  case llvm::Instruction::Add:
1983  case llvm::Instruction::Sub:
1984  case llvm::Instruction::Mul:
1985  // We need to cast integer arithmetic so that it is always performed
1986  // as unsigned, to avoid undefined behavior on overflow.
1987  case llvm::Instruction::LShr:
1988  case llvm::Instruction::UDiv:
1989  case llvm::Instruction::URem:
1990  shouldCast = true;
1991  break;
1992  case llvm::Instruction::AShr:
1993  case llvm::Instruction::SDiv:
1994  case llvm::Instruction::SRem:
1995  shouldCast = true;
1996  typeIsSigned = true;
1997  break;
1998  }
1999 
2000  // Write out the casted constant if we should, otherwise just write the
2001  // operand.
2002  if (shouldCast) {
2003  Out << "((";
2004  printSimpleType(Out, OpTy, typeIsSigned);
2005  Out << ")";
2006  printConstant(CPV, false);
2007  Out << ")";
2008  } else
2009  printConstant(CPV, false);
2010 }
2011 
2012 std::string CWriter::GetValueName(const llvm::Value *Operand) {
2013 
2014  // Resolve potential alias.
2015  if (const llvm::GlobalAlias *GA = llvm::dyn_cast<llvm::GlobalAlias>(Operand)) {
2016 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 /* LLVM 3.5+ */
2017  if (const llvm::Value *V = GA->getAliasee())
2018 #else /* <= LLVM 3.4 */
2019  if (const llvm::Value *V = GA->resolveAliasedGlobal(false))
2020 #endif
2021  Operand = V;
2022  }
2023 
2024  // Mangle globals with the standard mangler interface for LLC compatibility.
2025  if (const llvm::GlobalValue *GV = llvm::dyn_cast<llvm::GlobalValue>(Operand)) {
2026  (void)GV;
2027  //llvm::SmallString<128> Str;
2028  //Mang->getNameWithPrefix(Str, GV, false);
2029  //return CBEMangle(Str.str().str());
2030  return CBEMangle(Operand->getName().str().c_str());
2031  }
2032 
2033  std::string Name = Operand->getName();
2034 
2035  if (Name.empty()) { // Assign unique names to local temporaries.
2036  unsigned &No = AnonValueNumbers[Operand];
2037  if (No == 0)
2038  No = ++NextAnonValueNumber;
2039  Name = "tmp__" + llvm::utostr(No);
2040  }
2041 
2042  std::string VarName;
2043  VarName.reserve(Name.capacity());
2044 
2045  for (std::string::iterator I = Name.begin(), E = Name.end();
2046  I != E; ++I) {
2047  char ch = *I;
2048 
2049  if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
2050  (ch >= '0' && ch <= '9') || ch == '_')) {
2051  char buffer[5];
2052  sprintf(buffer, "_%x_", ch);
2053  VarName += buffer;
2054  } else
2055  VarName += ch;
2056  }
2057 
2058  if (llvm::isa<llvm::BasicBlock>(Operand))
2059  VarName += "_label";
2060  else
2061  VarName += "_";
2062 
2063  return VarName;
2064 }
2065 
2066 /// writeInstComputationInline - Emit the computation for the specified
2067 /// instruction inline, with no destination provided.
2068 void CWriter::writeInstComputationInline(llvm::Instruction &I) {
2069  // If this is a non-trivial bool computation, make sure to truncate down to
2070  // a 1 bit value. This is important because we want "add i1 x, y" to return
2071  // "0" when x and y are true, not "2" for example.
2072  bool NeedBoolTrunc = false;
2073  if (I.getType() == llvm::Type::getInt1Ty(I.getContext()) &&
2074  !llvm::isa<llvm::ICmpInst>(I) && !llvm::isa<llvm::FCmpInst>(I))
2075  NeedBoolTrunc = true;
2076 
2077  if (NeedBoolTrunc)
2078  Out << "((";
2079 
2080  visit(I);
2081 
2082  if (NeedBoolTrunc)
2083  Out << ")&1)";
2084 }
2085 
2086 
2087 void CWriter::writeOperandInternal(llvm::Value *Operand, bool Static) {
2088  if (llvm::Instruction *I = llvm::dyn_cast<llvm::Instruction>(Operand))
2089  // Should we inline this instruction to build a tree?
2090  if (isInlinableInst(*I) && !isDirectAlloca(I)) {
2091  Out << '(';
2092  writeInstComputationInline(*I);
2093  Out << ')';
2094  return;
2095  }
2096 
2097  llvm::Constant* CPV = llvm::dyn_cast<llvm::Constant>(Operand);
2098 
2099  if (CPV && !llvm::isa<llvm::GlobalValue>(CPV))
2100  printConstant(CPV, Static);
2101  else
2102  Out << GetValueName(Operand);
2103 }
2104 
2105 void CWriter::writeOperand(llvm::Value *Operand, bool Static) {
2106  bool isAddressImplicit = isAddressExposed(Operand);
2107  if (isAddressImplicit)
2108  Out << "(&"; // Global variables are referenced as their addresses by llvm
2109 
2110  writeOperandInternal(Operand, Static);
2111 
2112  if (isAddressImplicit)
2113  Out << ')';
2114 }
2115 
2116 // Some instructions need to have their result value casted back to the
2117 // original types because their operands were casted to the expected type.
2118 // This function takes care of detecting that case and printing the cast
2119 // for the Instruction.
2120 bool CWriter::writeInstructionCast(const llvm::Instruction &I) {
2121  llvm::Type *Ty = I.getOperand(0)->getType();
2122  switch (I.getOpcode()) {
2123  case llvm::Instruction::Add:
2124  case llvm::Instruction::Sub:
2125  case llvm::Instruction::Mul:
2126  // We need to cast integer arithmetic so that it is always performed
2127  // as unsigned, to avoid undefined behavior on overflow.
2128  case llvm::Instruction::LShr:
2129  case llvm::Instruction::URem:
2130  case llvm::Instruction::UDiv:
2131  Out << "((";
2132  printSimpleType(Out, Ty, false);
2133  Out << ")(";
2134  return true;
2135  case llvm::Instruction::AShr:
2136  case llvm::Instruction::SRem:
2137  case llvm::Instruction::SDiv:
2138  Out << "((";
2139  printSimpleType(Out, Ty, true);
2140  Out << ")(";
2141  return true;
2142  default: break;
2143  }
2144  return false;
2145 }
2146 
2147 // Write the operand with a cast to another type based on the Opcode being used.
2148 // This will be used in cases where an instruction has specific type
2149 // requirements (usually signedness) for its operands.
2150 void CWriter::writeOperandWithCast(llvm::Value* Operand, unsigned Opcode) {
2151 
2152  // Extract the operand's type, we'll need it.
2153  llvm::Type* OpTy = Operand->getType();
2154 
2155  // Indicate whether to do the cast or not.
2156  bool shouldCast = false;
2157 
2158  // Indicate whether the cast should be to a signed type or not.
2159  bool castIsSigned = false;
2160 
2161  // Based on the Opcode for which this Operand is being written, determine
2162  // the new type to which the operand should be casted by setting the value
2163  // of OpTy. If we change OpTy, also set shouldCast to true.
2164  switch (Opcode) {
2165  default:
2166  // for most instructions, it doesn't matter
2167  break;
2168  case llvm::Instruction::Add:
2169  case llvm::Instruction::Sub:
2170  case llvm::Instruction::Mul:
2171  // We need to cast integer arithmetic so that it is always performed
2172  // as unsigned, to avoid undefined behavior on overflow.
2173  case llvm::Instruction::LShr:
2174  case llvm::Instruction::UDiv:
2175  case llvm::Instruction::URem: // Cast to unsigned first
2176  shouldCast = true;
2177  castIsSigned = false;
2178  break;
2179  case llvm::Instruction::GetElementPtr:
2180  case llvm::Instruction::AShr:
2181  case llvm::Instruction::SDiv:
2182  case llvm::Instruction::SRem: // Cast to signed first
2183  shouldCast = true;
2184  castIsSigned = true;
2185  break;
2186  }
2187 
2188  // Write out the casted operand if we should, otherwise just write the
2189  // operand.
2190  if (shouldCast) {
2191  Out << "((";
2192  printSimpleType(Out, OpTy, castIsSigned);
2193  Out << ")";
2194  writeOperand(Operand);
2195  Out << ")";
2196  } else
2197  writeOperand(Operand);
2198 }
2199 
2200 // Write the operand with a cast to another type based on the icmp predicate
2201 // being used.
2202 void CWriter::writeOperandWithCast(llvm::Value* Operand, const llvm::ICmpInst &Cmp) {
2203  // This has to do a cast to ensure the operand has the right signedness.
2204  // Also, if the operand is a pointer, we make sure to cast to an integer when
2205  // doing the comparison both for signedness and so that the C compiler doesn't
2206  // optimize things like "p < NULL" to false (p may contain an integer value
2207  // f.e.).
2208  bool shouldCast = Cmp.isRelational();
2209 
2210  // Write out the casted operand if we should, otherwise just write the
2211  // operand.
2212  if (!shouldCast) {
2213  writeOperand(Operand);
2214  return;
2215  }
2216 
2217  // Should this be a signed comparison? If so, convert to signed.
2218  bool castIsSigned = Cmp.isSigned();
2219 
2220  // If the operand was a pointer, convert to a large integer type.
2221  llvm::Type* OpTy = Operand->getType();
2222  if (OpTy->isPointerTy())
2223  OpTy = TD->getIntPtrType(Operand->getContext());
2224 
2225  Out << "((";
2226  printSimpleType(Out, OpTy, castIsSigned);
2227  Out << ")";
2228  writeOperand(Operand);
2229  Out << ")";
2230 }
2231 
2232 // generateCompilerSpecificCode - This is where we add conditional compilation
2233 // directives to cater to specific compilers as need be.
2234 //
2235 static void generateCompilerSpecificCode(llvm::formatted_raw_ostream& Out,
2236  const llvm::DataLayout *TD) {
2237  // We output GCC specific attributes to preserve 'linkonce'ness on globals.
2238  // If we aren't being compiled with GCC, just drop these attributes.
2239  Out << "#ifndef __GNUC__ /* Can only support \"linkonce\" vars with GCC */\n"
2240  << "#define __attribute__(X)\n"
2241  << "#endif\n\n";
2242 
2243  // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))".
2244  Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
2245  << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n"
2246  << "#elif defined(__GNUC__)\n"
2247  << "#define __EXTERNAL_WEAK__ __attribute__((weak))\n"
2248  << "#else\n"
2249  << "#define __EXTERNAL_WEAK__\n"
2250  << "#endif\n\n";
2251 
2252  // For now, turn off the weak linkage attribute on Mac OS X. (See above.)
2253  Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
2254  << "#define __ATTRIBUTE_WEAK__\n"
2255  << "#elif defined(__GNUC__)\n"
2256  << "#define __ATTRIBUTE_WEAK__ __attribute__((weak))\n"
2257  << "#else\n"
2258  << "#define __ATTRIBUTE_WEAK__\n"
2259  << "#endif\n\n";
2260 
2261  // Add hidden visibility support. FIXME: APPLE_CC?
2262  Out << "#if defined(__GNUC__)\n"
2263  << "#define __HIDDEN__ __attribute__((visibility(\"hidden\")))\n"
2264  << "#endif\n\n";
2265 
2266  // Define NaN and Inf as GCC builtins if using GCC, as 0 otherwise
2267  // From the GCC documentation:
2268  //
2269  // double __builtin_nan (const char *str)
2270  //
2271  // This is an implementation of the ISO C99 function nan.
2272  //
2273  // Since ISO C99 defines this function in terms of strtod, which we do
2274  // not implement, a description of the parsing is in order. The string is
2275  // parsed as by strtol; that is, the base is recognized by leading 0 or
2276  // 0x prefixes. The number parsed is placed in the significand such that
2277  // the least significant bit of the number is at the least significant
2278  // bit of the significand. The number is truncated to fit the significand
2279  // field provided. The significand is forced to be a quiet NaN.
2280  //
2281  // This function, if given a string literal, is evaluated early enough
2282  // that it is considered a compile-time constant.
2283  //
2284  // float __builtin_nanf (const char *str)
2285  //
2286  // Similar to __builtin_nan, except the return type is float.
2287  //
2288  // double __builtin_inf (void)
2289  //
2290  // Similar to __builtin_huge_val, except a warning is generated if the
2291  // target floating-point format does not support infinities. This
2292  // function is suitable for implementing the ISO C99 macro INFINITY.
2293  //
2294  // float __builtin_inff (void)
2295  //
2296  // Similar to __builtin_inf, except the return type is float.
2297  Out << "#if (defined(__GNUC__) || defined(__clang__)) && !defined(__INTEL_COMPILER)\n"
2298  << "#define LLVM_NAN(NanStr) __builtin_nan(NanStr) /* Double */\n"
2299  << "#define LLVM_NANF(NanStr) __builtin_nanf(NanStr) /* Float */\n"
2300  << "#define LLVM_NANS(NanStr) __builtin_nans(NanStr) /* Double */\n"
2301  << "#define LLVM_NANSF(NanStr) __builtin_nansf(NanStr) /* Float */\n"
2302  << "#define LLVM_INF __builtin_inf() /* Double */\n"
2303  << "#define LLVM_INFF __builtin_inff() /* Float */\n"
2304  << "//#define LLVM_PREFETCH(addr,rw,locality) "
2305  "__builtin_prefetch(addr,rw,locality)\n"
2306  << "//#define __ATTRIBUTE_CTOR__ __attribute__((constructor))\n"
2307  << "//#define __ATTRIBUTE_DTOR__ __attribute__((destructor))\n"
2308  << "#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)\n"
2309  << "#include <limits>\n"
2310  << "#define LLVM_NAN(NanStr) std::numeric_limits<double>::quiet_NaN()\n"
2311  << "#define LLVM_NANF(NanStr) std::numeric_limits<float>::quiet_NaN()\n"
2312  << "#define LLVM_NANS(NanStr) std::numeric_limits<double>::signaling_NaN()\n"
2313  << "#define LLVM_NANSF(NanStr) std::numeric_limits<float>::signaling_NaN()\n"
2314  << "#define LLVM_INF std::numeric_limits<double>::infinity()\n"
2315  << "#define LLVM_INFF std::numeric_limits<float>::infinity()\n"
2316  << "//#define LLVM_PREFETCH(addr,rw,locality) /* PREFETCH */\n"
2317  << "//#define __ATTRIBUTE_CTOR__\n"
2318  << "//#define __ATTRIBUTE_DTOR__\n"
2319  << "#else\n"
2320  << "#error \"Not MSVC, clang, or g++?\"\n"
2321  << "#endif\n\n";
2322 
2323  // LLVM_ASM() is used to define mapping of the symbol to a different name,
2324  // this is expected to be MacOS-only feature. So defining it only for
2325  // gcc and clang (Intel Compiler on Linux/MacOS is also ok).
2326  // For example, this feature is required to translate symbols described in
2327  // "Symbol Variants Release Notes" document (on Apple website).
2328  Out << "#if (defined(__GNUC__) || defined(__clang__))\n"
2329  << "#define LLVM_ASM(X) __asm(X)\n"
2330  << "#endif\n\n";
2331 
2332  Out << "#if defined(__clang__) || defined(__INTEL_COMPILER) || "
2333  "(__GNUC__ < 4) /* Old GCCs, or compilers not GCC */ \n"
2334  << "#define __builtin_stack_save() 0 /* not implemented */\n"
2335  << "#define __builtin_stack_restore(X) /* noop */\n"
2336  << "#endif\n\n";
2337 
2338 #if 0
2339  // Output typedefs for 128-bit integers. If these are needed with a
2340  // 32-bit target or with a C compiler that doesn't support mode(TI),
2341  // more drastic measures will be needed.
2342  Out << "#if __GNUC__ && __LP64__ /* 128-bit integer types */\n"
2343  << "typedef int __attribute__((mode(TI))) llvmInt128;\n"
2344  << "typedef unsigned __attribute__((mode(TI))) llvmUInt128;\n"
2345  << "#endif\n\n";
2346 #endif
2347 
2348  // Output target-specific code that should be inserted into main.
2349  Out << "#define CODE_FOR_MAIN() /* Any target-specific code for main()*/\n";
2350 }
2351 
2352 /// FindStaticTors - Given a static ctor/dtor list, unpack its contents into
2353 /// the StaticTors set.
2354 static void FindStaticTors(llvm::GlobalVariable *GV, std::set<llvm::Function*> &StaticTors){
2355  llvm::ConstantArray *InitList = llvm::dyn_cast<llvm::ConstantArray>(GV->getInitializer());
2356  if (!InitList) return;
2357 
2358  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
2359  if (llvm::ConstantStruct *CS = llvm::dyn_cast<llvm::ConstantStruct>(InitList->getOperand(i))){
2360  if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
2361 
2362  if (CS->getOperand(1)->isNullValue())
2363  return; // Found a null terminator, exit printing.
2364  llvm::Constant *FP = CS->getOperand(1);
2365  if (llvm::ConstantExpr *CE = llvm::dyn_cast<llvm::ConstantExpr>(FP))
2366  if (CE->isCast())
2367  FP = CE->getOperand(0);
2368  if (llvm::Function *F = llvm::dyn_cast<llvm::Function>(FP))
2369  StaticTors.insert(F);
2370  }
2371 }
2372 
2377 };
2378 
2379 /// getGlobalVariableClass - If this is a global that is specially recognized
2380 /// by LLVM, return a code that indicates how we should handle it.
2381 static SpecialGlobalClass getGlobalVariableClass(const llvm::GlobalVariable *GV) {
2382  // If this is a global ctors/dtors list, handle it now.
2383  if (GV->hasAppendingLinkage() && GV->use_empty()) {
2384  if (GV->getName() == "llvm.global_ctors")
2385  return GlobalCtors;
2386  else if (GV->getName() == "llvm.global_dtors")
2387  return GlobalDtors;
2388  }
2389 
2390  // Otherwise, if it is other metadata, don't print it. This catches things
2391  // like debug information.
2392 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_8 /* LLVM 3.5-3.8 */
2393  // Here we compare char *
2394  if (!strcmp(GV->getSection(), "llvm.metadata"))
2395 #else
2396  // Here we compare strings
2397  if (GV->getSection() == "llvm.metadata")
2398 #endif
2399  return NotPrinted;
2400 
2401  return NotSpecial;
2402 }
2403 
2404 // PrintEscapedString - Print each character of the specified string, escaping
2405 // it if it is not printable or if it is an escape char.
2406 static void PrintEscapedString(const char *Str, unsigned Length,
2407  llvm::raw_ostream &Out) {
2408  for (unsigned i = 0; i != Length; ++i) {
2409  unsigned char C = Str[i];
2410  if (isprint(C) && C != '\\' && C != '"')
2411  Out << C;
2412  else if (C == '\\')
2413  Out << "\\\\";
2414  else if (C == '\"')
2415  Out << "\\\"";
2416  else if (C == '\t')
2417  Out << "\\t";
2418  else
2419  Out << "\\x" << llvm::hexdigit(C >> 4) << llvm::hexdigit(C & 0x0F);
2420  }
2421 }
2422 
2423 // PrintEscapedString - Print each character of the specified string, escaping
2424 // it if it is not printable or if it is an escape char.
2425 static void PrintEscapedString(const std::string &Str, llvm::raw_ostream &Out) {
2426  PrintEscapedString(Str.c_str(), Str.size(), Out);
2427 }
2428 
2429 bool CWriter::doInitialization(llvm::Module &M) {
2430  llvm::FunctionPass::doInitialization(M);
2431 
2432  // Initialize
2433  TheModule = &M;
2434 
2435  TD = new llvm::DataLayout(&M);
2436  IL = new llvm::IntrinsicLowering(*TD);
2437  IL->AddPrototypes(M);
2438 
2439 #if 0
2440  std::string Triple = TheModule->getTargetTriple();
2441  if (Triple.empty())
2442  Triple = llvm::sys::getDefaultTargetTriple();
2443 
2444  std::string E;
2445  if (const llvm::Target *Match = llvm::TargetRegistry::lookupTarget(Triple, E))
2446  TAsm = Match->createMCAsmInfo(Triple);
2447 #endif
2448  TAsm = new CBEMCAsmInfo();
2449  MRI = new llvm::MCRegisterInfo();
2450 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_4 // LLVM 3.4+
2451  TCtx = new llvm::MCContext(TAsm, MRI, NULL);
2452 #else
2453  TCtx = new llvm::MCContext(*TAsm, *MRI, NULL);
2454 #endif
2455  //Mang = new llvm::Mangler(*TCtx, *TD);
2456 
2457  // Keep track of which functions are static ctors/dtors so they can have
2458  // an attribute added to their prototypes.
2459  std::set<llvm::Function*> StaticCtors, StaticDtors;
2460  for (llvm::Module::global_iterator I = M.global_begin(), E = M.global_end();
2461  I != E; ++I) {
2462 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
2463  switch (getGlobalVariableClass(I)) {
2464 #else /* LLVM 3.8+ */
2465  switch (getGlobalVariableClass(&*I)) {
2466 #endif
2467  default: break;
2468  case GlobalCtors:
2469  FindStaticTors(&*I, StaticCtors);
2470  break;
2471  case GlobalDtors:
2472  FindStaticTors(&*I, StaticDtors);
2473  break;
2474  }
2475  }
2476 
2477  Out << "/*******************************************************************\n";
2478  Out << " This file has been automatically generated by ispc\n";
2479  Out << " DO NOT EDIT THIS FILE DIRECTLY\n";
2480  Out << " *******************************************************************/\n\n";
2481 
2482  Out << "/* Provide Declarations */\n";
2483  Out << "#include <stdarg.h>\n"; // Varargs support
2484  Out << "#include <setjmp.h>\n"; // Unwind support
2485  Out << "#include <limits.h>\n"; // With overflow intrinsics support.
2486  Out << "#include <stdlib.h>\n";
2487  Out << "#ifdef _MSC_VER\n";
2488  Out << " #define NOMINMAX\n";
2489  Out << " #include <windows.h>\n";
2490  Out << "#endif // _MSC_VER\n";
2491  Out << "#include <stdlib.h>\n";
2492  Out << "#include <stdint.h>\n";
2493  Out << "/* get a declaration for alloca */\n";
2494  Out << "#ifdef _MSC_VER\n";
2495  Out << " #include <malloc.h>\n";
2496  Out << " #define alloca _alloca\n";
2497  Out << "#else\n";
2498  Out << " #include <alloca.h>\n";
2499  Out << "#endif\n\n";
2500 
2501  if (g->opt.fastMath) {
2502  Out << "#define ISPC_FAST_MATH 1\n";
2503  } else {
2504  Out << "#undef ISPC_FAST_MATH\n";
2505  }
2506 
2507  if (g->opt.forceAlignedMemory) {
2508  Out << "#define ISPC_FORCE_ALIGNED_MEMORY\n";
2509  }
2510 
2511  Out << "#include \"" << includeName << "\"\n";
2512 
2513  Out << "\n/* Basic Library Function Declarations */\n";
2514  Out << "extern \"C\" {\n";
2515  Out << "int puts(unsigned char *);\n";
2516  Out << "unsigned int putchar(unsigned int);\n";
2517  Out << "int fflush(void *);\n";
2518  Out << "int printf(const unsigned char *, ...);\n";
2519  Out << "uint8_t *memcpy(uint8_t *, uint8_t *, uint64_t );\n";
2520  Out << "uint8_t *memset(uint8_t *, uint8_t, uint64_t );\n";
2521  Out << "void memset_pattern16(void *, const void *, uint64_t );\n";
2522  Out << "}\n\n";
2523 
2525 
2526  // Provide a definition for `bool' if not compiling with a C++ compiler.
2527  Out << "\n"
2528  << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
2529 
2530  << "\n\n/* Support for floating point constants */\n"
2531  << "typedef uint64_t ConstantDoubleTy;\n"
2532  << "typedef uint32_t ConstantFloatTy;\n"
2533  << "typedef struct { unsigned long long f1; unsigned short f2; "
2534  "unsigned short pad[3]; } ConstantFP80Ty;\n"
2535  // This is used for both kinds of 128-bit long double; meaning differs.
2536  << "typedef struct { uint64_t f1, f2; } ConstantFP128Ty;\n"
2537  << "\n\n/* Global Declarations */\n\n";
2538 
2539  // First output all the declarations for the program, because C requires
2540  // Functions & globals to be declared before they are used.
2541  //
2542  if (!M.getModuleInlineAsm().empty()) {
2543  Out << "/* Module asm statements */\n"
2544  << "asm(";
2545 
2546  // Split the string into lines, to make it easier to read the .ll file.
2547  std::string Asm = M.getModuleInlineAsm();
2548  size_t CurPos = 0;
2549  size_t NewLine = Asm.find_first_of('\n', CurPos);
2550  while (NewLine != std::string::npos) {
2551  // We found a newline, print the portion of the asm string from the
2552  // last newline up to this newline.
2553  Out << "\"";
2554  PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
2555  Out);
2556  Out << "\\n\"\n";
2557  CurPos = NewLine+1;
2558  NewLine = Asm.find_first_of('\n', CurPos);
2559  }
2560  Out << "\"";
2561  PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
2562  Out << "\");\n"
2563  << "/* End Module asm statements */\n";
2564  }
2565 
2566  // Loop over the symbol table, emitting all named constants.
2567  printModuleTypes();
2568 
2569  // Global variable declarations...
2570  if (!M.global_empty()) {
2571  Out << "\n/* External Global Variable Declarations */\n";
2572  for (llvm::Module::global_iterator I = M.global_begin(), E = M.global_end();
2573  I != E; ++I) {
2574 
2575  if (I->hasExternalLinkage() || I->hasExternalWeakLinkage() ||
2576  I->hasCommonLinkage())
2577  Out << "extern ";
2578 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
2579  else if (I->hasDLLImportStorageClass())
2580 #else
2581  else if (I->hasDLLImportLinkage())
2582 #endif
2583  Out << "__declspec(dllimport) ";
2584  else
2585  continue; // Internal Global
2586 
2587  // Thread Local Storage
2588  if (I->isThreadLocal())
2589  Out << "__thread ";
2590 
2591  printType(Out, I->getType()->getElementType(), false, GetValueName(&*I));
2592 
2593  if (I->hasExternalWeakLinkage())
2594  Out << " __EXTERNAL_WEAK__";
2595  Out << ";\n";
2596  }
2597  }
2598 
2599  // Output the global variable declarations
2600  if (!M.global_empty()) {
2601  Out << "\n\n/* Global Variable Declarations */\n";
2602  for (llvm::Module::global_iterator I = M.global_begin(), E = M.global_end();
2603  I != E; ++I)
2604  if (!I->isDeclaration()) {
2605  // Ignore special globals, such as debug info.
2606  if (getGlobalVariableClass(&*I))
2607  continue;
2608 
2609  if (I->hasLocalLinkage())
2610  continue;
2611  else
2612  Out << "extern ";
2613 
2614  // Thread Local Storage
2615  if (I->isThreadLocal())
2616  Out << "__thread ";
2617 
2618  printType(Out, I->getType()->getElementType(), false,
2619  GetValueName(&*I));
2620 
2621  if (I->hasLinkOnceLinkage())
2622  Out << " __attribute__((common))";
2623  else if (I->hasCommonLinkage()) // FIXME is this right?
2624  Out << " __ATTRIBUTE_WEAK__";
2625  else if (I->hasWeakLinkage())
2626  Out << " __ATTRIBUTE_WEAK__";
2627  else if (I->hasExternalWeakLinkage())
2628  Out << " __EXTERNAL_WEAK__";
2629  if (I->hasHiddenVisibility())
2630  Out << " __HIDDEN__";
2631  Out << ";\n";
2632  }
2633  }
2634 
2635  // Function declarations
2636  Out << "\n/* Function Declarations */\n";
2637  Out << "extern \"C\" {\n";
2638 
2639  // Store the intrinsics which will be declared/defined below.
2640  llvm::SmallVector<const llvm::Function*, 8> intrinsicsToDefine;
2641 
2642  for (llvm::Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
2643  // Don't print declarations for intrinsic functions.
2644  // Store the used intrinsics, which need to be explicitly defined.
2645  if (I->isIntrinsic()) {
2646  switch (I->getIntrinsicID()) {
2647  default:
2648  break;
2649  case llvm::Intrinsic::uadd_with_overflow:
2650  case llvm::Intrinsic::sadd_with_overflow:
2651  case llvm::Intrinsic::umul_with_overflow:
2652 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
2653  intrinsicsToDefine.push_back(I);
2654 #else /* LLVM 3.8+ */
2655  intrinsicsToDefine.push_back(&*I);
2656 #endif
2657  break;
2658  }
2659  continue;
2660  }
2661 
2662  if (I->getName() == "setjmp" || I->getName() == "abort" ||
2663  I->getName() == "longjmp" || I->getName() == "_setjmp" ||
2664  I->getName() == "memset" || I->getName() == "memset_pattern16" ||
2665  I->getName() == "puts" ||
2666  I->getName() == "printf" || I->getName() == "putchar" ||
2667  I->getName() == "fflush" ||
2668  // Memory allocation
2669  I->getName() == "malloc" ||
2670  I->getName() == "posix_memalign" ||
2671  I->getName() == "free" ||
2672  I->getName() == "_aligned_malloc" ||
2673  I->getName() == "_aligned_free"
2674  )
2675  continue;
2676 
2677  // Don't redeclare ispc's own intrinsics
2678  std::string name = I->getName();
2679  if (name.size() > 2 && name[0] == '_' && name[1] == '_')
2680  continue;
2681 
2682  if (I->hasExternalWeakLinkage())
2683  Out << "extern ";
2684 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
2685  printFunctionSignature(I, true);
2686 #else /* LLVM 3.8+ */
2687  printFunctionSignature(&*I, true);
2688 #endif
2689  if (I->hasWeakLinkage() || I->hasLinkOnceLinkage())
2690  Out << " __ATTRIBUTE_WEAK__";
2691  if (I->hasExternalWeakLinkage())
2692  Out << " __EXTERNAL_WEAK__";
2693 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
2694  if (StaticCtors.count(I))
2695  Out << " __ATTRIBUTE_CTOR__";
2696  if (StaticDtors.count(I))
2697 #else /* LLVM 3.8+ */
2698  if (StaticCtors.count(&*I))
2699  Out << " __ATTRIBUTE_CTOR__";
2700  if (StaticDtors.count(&*I))
2701 #endif
2702  Out << " __ATTRIBUTE_DTOR__";
2703  if (I->hasHiddenVisibility())
2704  Out << " __HIDDEN__";
2705 
2706  // This is MacOS specific feature, this should not appear on other platforms.
2707  if (I->hasName() && I->getName()[0] == 1)
2708  Out << " LLVM_ASM(\"" << I->getName().substr(1) << "\")";
2709 
2710  Out << ";\n";
2711  }
2712  Out << "}\n\n";
2713 
2714  if (!M.empty())
2715  Out << "\n\n/* Function Bodies */\n";
2716 
2717  // Emit some helper functions for dealing with FCMP instruction's
2718  // predicates
2719  Out << "template <typename A, typename B> static inline int llvm_fcmp_ord(A X, B Y) { ";
2720  Out << "return X == X && Y == Y; }\n";
2721  Out << "template <typename A, typename B> static inline int llvm_fcmp_uno(A X, B Y) { ";
2722  Out << "return X != X || Y != Y; }\n";
2723  Out << "template <typename A, typename B> static inline int llvm_fcmp_ueq(A X, B Y) { ";
2724  Out << "return X == Y || llvm_fcmp_uno(X, Y); }\n";
2725  Out << "template <typename A, typename B> static inline int llvm_fcmp_une(A X, B Y) { ";
2726  Out << "return X != Y; }\n";
2727  Out << "template <typename A, typename B> static inline int llvm_fcmp_ult(A X, B Y) { ";
2728  Out << "return X < Y || llvm_fcmp_uno(X, Y); }\n";
2729  Out << "template <typename A, typename B> static inline int llvm_fcmp_ugt(A X, B Y) { ";
2730  Out << "return X > Y || llvm_fcmp_uno(X, Y); }\n";
2731  Out << "template <typename A, typename B> static inline int llvm_fcmp_ule(A X, B Y) { ";
2732  Out << "return X <= Y || llvm_fcmp_uno(X, Y); }\n";
2733  Out << "template <typename A, typename B> static inline int llvm_fcmp_uge(A X, B Y) { ";
2734  Out << "return X >= Y || llvm_fcmp_uno(X, Y); }\n";
2735  Out << "template <typename A, typename B> static inline int llvm_fcmp_oeq(A X, B Y) { ";
2736  Out << "return X == Y ; }\n";
2737  Out << "template <typename A, typename B> static inline int llvm_fcmp_one(A X, B Y) { ";
2738  Out << "return X != Y && llvm_fcmp_ord(X, Y); }\n";
2739  Out << "template <typename A, typename B> static inline int llvm_fcmp_olt(A X, B Y) { ";
2740  Out << "return X < Y ; }\n";
2741  Out << "template <typename A, typename B> static inline int llvm_fcmp_ogt(A X, B Y) { ";
2742  Out << "return X > Y ; }\n";
2743  Out << "template <typename A, typename B> static inline int llvm_fcmp_ole(A X, B Y) { ";
2744  Out << "return X <= Y ; }\n";
2745  Out << "template <typename A, typename B> static inline int llvm_fcmp_oge(A X, B Y) { ";
2746  Out << "return X >= Y ; }\n";
2747  Out << "template <typename A> A *Memset(A *ptr, int count, size_t len) { ";
2748  Out << "return (A *)memset(ptr, count, len); }\n";
2749 
2750  // Emit definitions of the intrinsics.
2751  for (llvm::SmallVector<const llvm::Function*, 8>::const_iterator
2752  I = intrinsicsToDefine.begin(),
2753  E = intrinsicsToDefine.end(); I != E; ++I) {
2754  printIntrinsicDefinition(**I, Out);
2755  }
2756 
2757  // Output the global variable definitions and contents...
2758  if (!M.global_empty()) {
2759  Out << "\n\n/* Global Variable Definitions and Initialization */\n";
2760  for (llvm::Module::global_iterator I = M.global_begin(), E = M.global_end();
2761  I != E; ++I)
2762  if (!I->isDeclaration()) {
2763  // Ignore special globals, such as debug info.
2764 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
2765  if (getGlobalVariableClass(I))
2766 #else /* LLVM 3.8+ */
2767  if (getGlobalVariableClass(&*I))
2768 #endif
2769  continue;
2770 
2771  if (I->hasLocalLinkage())
2772  Out << "static ";
2773 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
2774  else if (I->hasDLLImportStorageClass()) Out << "__declspec(dllimport) ";
2775  else if (I->hasDLLExportStorageClass()) Out << "__declspec(dllexport) ";
2776 #else
2777  else if (I->hasDLLImportLinkage()) Out << "__declspec(dllimport) ";
2778  else if (I->hasDLLExportLinkage()) Out << "__declspec(dllexport) ";
2779 #endif
2780  // Thread Local Storage
2781  if (I->isThreadLocal())
2782  Out << "__thread ";
2783 
2784  printType(Out, I->getType()->getElementType(), false,
2785 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
2786  GetValueName(I));
2787 #else /* LLVM 3.8+ */
2788  GetValueName(&*I));
2789 #endif
2790 
2791  if (I->hasLinkOnceLinkage())
2792  Out << " __attribute__((common))";
2793  else if (I->hasWeakLinkage())
2794  Out << " __ATTRIBUTE_WEAK__";
2795  else if (I->hasCommonLinkage())
2796  Out << " __ATTRIBUTE_WEAK__";
2797 
2798  if (I->hasHiddenVisibility())
2799  Out << " __HIDDEN__";
2800 
2801  // If the initializer is not null, emit the initializer. If it is null,
2802  // we try to avoid emitting large amounts of zeros. The problem with
2803  // this, however, occurs when the variable has weak linkage. In this
2804  // case, the assembler will complain about the variable being both weak
2805  // and common, so we disable this optimization.
2806  // FIXME common linkage should avoid this problem.
2807  if (!I->getInitializer()->isNullValue()) {
2808  Out << " = " ;
2809 
2810  // vec16_i64 should be handled separately
2811  if (is_vec16_i64_ty(I->getType()->getElementType())) {
2812  Out << "/* vec16_i64 should be loaded carefully on knc */\n";
2813  Out << "\n#if defined(KNC) \n";
2814  Out << "hilo2zmm";
2815  Out << "\n#endif \n";
2816  }
2817 
2818  Out << "(";
2819  writeOperand(I->getInitializer(), false);
2820  Out << ")";
2821  } else if (I->hasWeakLinkage()) {
2822  // We have to specify an initializer, but it doesn't have to be
2823  // complete. If the value is an aggregate, print out { 0 }, and let
2824  // the compiler figure out the rest of the zeros.
2825  Out << " = " ;
2826  if (I->getInitializer()->getType()->isStructTy() ||
2827  I->getInitializer()->getType()->isVectorTy()) {
2828  Out << "{ 0 }";
2829  } else if (I->getInitializer()->getType()->isArrayTy()) {
2830  // As with structs and vectors, but with an extra set of braces
2831  // because arrays are wrapped in structs.
2832  Out << "{ { 0 } }";
2833  } else {
2834  // Just print it out normally.
2835  writeOperand(I->getInitializer(), false);
2836  }
2837  }
2838  Out << ";\n";
2839  }
2840  }
2841 
2842  return false;
2843 }
2844 
2845 
2846 /// Output all floating point constants that cannot be printed accurately...
2847 void CWriter::printFloatingPointConstants(llvm::Function &F) {
2848  // Scan the module for floating point constants. If any FP constant is used
2849  // in the function, we want to redirect it here so that we do not depend on
2850  // the precision of the printed form, unless the printed form preserves
2851  // precision.
2852  //
2854  E = constant_scanner::constant_end(&F); I != E; ++I)
2855  printFloatingPointConstants(*I);
2856 
2857  Out << '\n';
2858 }
2859 
2860 void CWriter::printFloatingPointConstants(const llvm::Constant *C) {
2861  // If this is a constant expression, recursively check for constant fp values.
2862  if (const llvm::ConstantExpr *CE = llvm::dyn_cast<llvm::ConstantExpr>(C)) {
2863  for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
2864  printFloatingPointConstants(CE->getOperand(i));
2865  return;
2866  }
2867 
2868  // Otherwise, check for a FP constant that we need to print.
2869  const llvm::ConstantFP *FPC = llvm::dyn_cast<llvm::ConstantFP>(C);
2870  if (FPC == 0 ||
2871  // Do not put in FPConstantMap if safe.
2872  isFPCSafeToPrint(FPC) ||
2873  // Already printed this constant?
2874  FPConstantMap.count(FPC))
2875  return;
2876 
2877  FPConstantMap[FPC] = FPCounter; // Number the FP constants
2878 
2879  if (FPC->getType() == llvm::Type::getDoubleTy(FPC->getContext())) {
2880  double Val = FPC->getValueAPF().convertToDouble();
2881  uint64_t i = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2882  Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
2883  << " = 0x" << llvm::utohexstr(i)
2884  << "ULL; /* " << Val << " */\n";
2885  } else if (FPC->getType() == llvm::Type::getFloatTy(FPC->getContext())) {
2886  float Val = FPC->getValueAPF().convertToFloat();
2887  uint32_t i = (uint32_t)FPC->getValueAPF().bitcastToAPInt().
2888  getZExtValue();
2889  Out << "static const ConstantFloatTy FPConstant" << FPCounter++
2890  << " = 0x" << llvm::utohexstr(i)
2891  << "U; /* " << Val << " */\n";
2892  } else if (FPC->getType() == llvm::Type::getX86_FP80Ty(FPC->getContext())) {
2893  // api needed to prevent premature destruction
2894  llvm::APInt api = FPC->getValueAPF().bitcastToAPInt();
2895  const uint64_t *p = api.getRawData();
2896  Out << "static const ConstantFP80Ty FPConstant" << FPCounter++
2897  << " = { 0x" << llvm::utohexstr(p[0])
2898  << "ULL, 0x" << llvm::utohexstr((uint16_t)p[1]) << ",{0,0,0}"
2899  << "}; /* Long double constant */\n";
2900  } else if (FPC->getType() == llvm::Type::getPPC_FP128Ty(FPC->getContext()) ||
2901  FPC->getType() == llvm::Type::getFP128Ty(FPC->getContext())) {
2902  llvm::APInt api = FPC->getValueAPF().bitcastToAPInt();
2903  const uint64_t *p = api.getRawData();
2904  Out << "static const ConstantFP128Ty FPConstant" << FPCounter++
2905  << " = { 0x"
2906  << llvm::utohexstr(p[0]) << ", 0x" << llvm::utohexstr(p[1])
2907  << "}; /* Long double constant */\n";
2908 
2909  } else {
2910  llvm_unreachable("Unknown float type!");
2911  }
2912 }
2913 
2914 
2915 // For any vector constants, generate code to declare static const arrays
2916 // with their element values. Doing so allows us to emit aligned vector
2917 // loads to get their values, rather than tediously inserting the
2918 // individual values into the vector.
2919 void CWriter::printVectorConstants(llvm::Function &F) {
2921  E = constant_scanner::constant_end(&F); I != E; ++I) {
2922  const llvm::ConstantDataVector *CDV = llvm::dyn_cast<llvm::ConstantDataVector>(*I);
2923  if (CDV == NULL)
2924  continue;
2925 
2926  // Don't bother if this is a splat of the same value; a (more
2927  // efficient?) __splat_* call will be generated for these.
2928  if (CDV->getSplatValue() != NULL)
2929  continue;
2930 
2931  // Don't align to anything more than 64 bytes
2932  int alignment = 4 * std::min(vectorWidth, 16);
2933 
2934  Out << "static const ";
2935  printSimpleType(Out, CDV->getElementType(), true, "");
2936  Out << "__attribute__ ((aligned(" << alignment << "))) ";
2937  Out << "VectorConstant" << VectorConstantIndex << "[] = { ";
2938  for (int i = 0; i < (int)CDV->getNumElements(); ++i) {
2939  printConstant(CDV->getElementAsConstant(i), false);
2940  Out << ", ";
2941  }
2942  Out << " };\n";
2943 
2944  VectorConstantMap[CDV] = VectorConstantIndex++;
2945  }
2946  Out << "\n";
2947 }
2948 
2949 /// printSymbolTable - Run through symbol table looking for type names. If a
2950 /// type name is found, emit its declaration...
2951 ///
2952 void CWriter::printModuleTypes() {
2953  Out << "\n/* Helper union for bitcasts */\n";
2954  Out << "typedef union {\n";
2955  Out << " unsigned int Int32;\n";
2956  Out << " unsigned long long Int64;\n";
2957  Out << " float Float;\n";
2958  Out << " double Double;\n";
2959  Out << "} llvmBitCastUnion;\n";
2960  Out << "\n/* This is special class, designed for operations with long int.*/ \n";
2961  Out << "namespace { \n";
2962  Out << "template <int num_bits> \n";
2963  Out << "struct iN { \n";
2964  Out << " int num[num_bits / (sizeof (int) * 8)]; \n";
2965  Out << " \n";
2966  Out << " iN () {} \n";
2967  Out << " \n";
2968  Out << " iN (const char *val) { \n";
2969  Out << " if (val == NULL) \n";
2970  Out << " return; \n";
2971  Out << " int length = num_bits / (sizeof (int) * 8); \n";
2972  Out << " int val_len = 0; \n";
2973  Out << " for (val_len = 0; val[val_len]; (val_len)++); \n";
2974  Out << " for (int i = 0; (i < val_len && i < num_bits); i++) \n";
2975  Out << " num[i / (sizeof (int) * 8)] = (num[i / (sizeof (int) * 8)] << 1) | (val[i] - '0'); \n";
2976  Out << " } \n";
2977  Out << " \n";
2978  Out << " ~iN () {} \n";
2979  Out << " \n";
2980  Out << " iN operator >> (const iN rhs) { \n";
2981  Out << " iN res; \n";
2982  Out << " int length = num_bits / (sizeof (int) * 8); \n";
2983  Out << " int cells_shift = rhs.num[0] / (sizeof(int) * 8); \n";
2984  Out << " int small_shift = rhs.num[0] % (sizeof(int) * 8); \n";
2985  Out << " for (int i = 0; i < (length - cells_shift); i++) \n";
2986  Out << " res.num[i] = this->num[cells_shift + i]; \n";
2987  Out << " for (int i = 0; i < length - 1; i++) { \n";
2988  Out << " res.num[i] = this->num[i] >> small_shift; \n";
2989  Out << " res.num[i] = ((this->num[i + 1] << ((sizeof(int) * 8) - small_shift))) | res.num[i];\n";
2990  Out << " } \n";
2991  Out << " res.num[length - 1] = res.num[length - 1] >> small_shift; \n";
2992  Out << " return res; \n";
2993  Out << " } \n";
2994  Out << " \n";
2995  Out << " iN operator & (iN rhs) { \n";
2996  Out << " iN res; \n";
2997  Out << " int length = num_bits / (sizeof (int) * 8); \n";
2998  Out << " for (int i = 0; i < length; i++) \n";
2999  Out << " res.num[i] = (this->num[i]) & (rhs.num[i]); \n";
3000  Out << " return res; \n";
3001  Out << " } \n";
3002  Out << " \n";
3003  Out << " operator uint32_t() { return this->num[0]; } \n";
3004  Out << " \n";
3005  Out << " template <class T> \n";
3006  Out << " friend iN<num_bits> __cast_bits(iN<num_bits> to, T from) { \n";
3007  Out << " for (int i = 0; i <" << vectorWidth << "; i++) \n";
3008  Out << " to.num[i] = ((int*)(&from))[i]; \n";
3009  Out << " return to; \n";
3010  Out << " } \n";
3011  Out << " \n";
3012  Out << " template <class T> \n";
3013  Out << " friend T __cast_bits(T to, iN<num_bits> from) { \n";
3014  Out << " for (int i = 0; i <" << vectorWidth << "; i++) \n";
3015  Out << " ((int*)(&to))[i] = from.num[i]; \n";
3016  Out << " return to; \n";
3017  Out << " } \n";
3018  Out << " \n";
3019  Out << " template <int ALIGN, class T> \n";
3020  Out << " friend void __store(T *p, iN<num_bits> val) { \n";
3021  Out << " for (int i = 0; i <" << vectorWidth << "; i++) \n";
3022  Out << " ((int*)p)[i] = val.num[i]; \n";
3023  Out << " } \n";
3024  Out << "}; \n";
3025  Out << "};\n";
3026  Out << "\n";
3027 
3028  // Get all of the struct types used in the module.
3029  std::vector<llvm::StructType*> StructTypes;
3030  llvm::TypeFinder typeFinder;
3031  typeFinder.run(*TheModule, false);
3032  for (llvm::TypeFinder::iterator iter = typeFinder.begin();
3033  iter != typeFinder.end(); ++iter)
3034  StructTypes.push_back(*iter);
3035 
3036  // Get all of the array types used in the module
3037  std::vector<llvm::ArrayType*> ArrayTypes;
3038  std::vector<llvm::IntegerType*> IntegerTypes;
3039  std::vector<bool> IsVolatile;
3040  std::vector<int> Alignment;
3041 
3042  findUsedArrayAndLongIntTypes(TheModule, ArrayTypes, IntegerTypes, IsVolatile, Alignment);
3043 
3044  if (StructTypes.empty() && ArrayTypes.empty())
3045  return;
3046 
3047  Out << "/* Structure and array forward declarations */\n";
3048 
3049  unsigned NextTypeID = 0;
3050 
3051  // If any of them are missing names, add a unique ID to UnnamedStructIDs.
3052  // Print out forward declarations for structure types.
3053  for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
3054  llvm::StructType *ST = StructTypes[i];
3055 
3056  if (ST->isLiteral() || ST->getName().empty())
3057  UnnamedStructIDs[ST] = NextTypeID++;
3058 
3059  std::string Name = getStructName(ST);
3060 
3061  Out << "struct " << Name << ";\n";
3062  }
3063 
3064  Out << "namespace {\n";
3065  for (unsigned i = 0, e = ArrayTypes.size(); i != e; ++i) {
3066  llvm::ArrayType *AT = ArrayTypes[i];
3067  ArrayIDs[AT] = NextTypeID++;
3068  std::string Name = getArrayName(AT);
3069  Out << " struct " << Name << ";\n";
3070  }
3071  Out << "};\n";
3072 
3073  for (unsigned i = 0, e = IntegerTypes.size(); i != e; ++i) {
3074  llvm::IntegerType *IT = IntegerTypes[i];
3075  if (IT->getIntegerBitWidth() <= 64 || Alignment[i] == 0)
3076  continue;
3077 
3078  Out << "typedef struct __attribute__ ((packed, aligned(" << Alignment[i] << "))) {\n ";
3079  IsVolatile[i] ? Out << " volatile " : Out << " ";
3080  printType(Out, IT, false, "data");
3081  Out << ";\n";
3082  Out << "} iN_" << IT->getIntegerBitWidth() << "_align_" << Alignment[i] << ";\n";
3083  }
3084 
3085  Out << '\n';
3086 
3087  // Keep track of which types have been printed so far.
3088  llvm::SmallPtrSet<llvm::Type *, 16> StructArrayPrinted;
3089 
3090  // Loop over all structures then push them into the stack so they are
3091  // printed in the correct order.
3092  //
3093  Out << "/* Structure and array contents */\n";
3094  for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
3095  if (StructTypes[i]->isStructTy())
3096  // Only print out used types!
3097  printContainedStructs(StructTypes[i], StructArrayPrinted);
3098  }
3099 
3100  Out << "namespace {\n";
3101  for (unsigned i = 0, e = ArrayTypes.size(); i != e; ++i)
3102  printContainedArrays(ArrayTypes[i], StructArrayPrinted);
3103 
3104  Out << "};\n";
3105  Out << '\n';
3106 }
3107 
3108 // Push the struct onto the stack and recursively push all structs
3109 // this one depends on.
3110 //
3111 // TODO: Make this work properly with vector types
3112 //
3113 void CWriter::printContainedStructs(llvm::Type *Ty,
3114  llvm::SmallPtrSet<llvm::Type *, 16> &Printed) {
3115  // Don't walk through pointers.
3116  if (!(Ty->isStructTy() || Ty->isArrayTy()))
3117  return;
3118 
3119  // Print all contained types first.
3120  for (llvm::Type::subtype_iterator I = Ty->subtype_begin(),
3121  E = Ty->subtype_end(); I != E; ++I)
3122  printContainedStructs(*I, Printed);
3123 
3124  if (llvm::StructType *ST = llvm::dyn_cast<llvm::StructType>(Ty)) {
3125  // Check to see if we have already printed this struct.
3126 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
3127  if (!Printed.insert(Ty).second) return;
3128 #else
3129  if (!Printed.insert(Ty)) return;
3130 #endif
3131 
3132  // Print structure type out.
3133  printType(Out, ST, false, getStructName(ST), true);
3134  Out << ";\n\n";
3135  }
3136  if (llvm::ArrayType *AT = llvm::dyn_cast<llvm::ArrayType>(Ty)) {
3137 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
3138  if (!Printed.insert(Ty).second) return;
3139 #else
3140  if (!Printed.insert(Ty)) return;
3141 #endif
3142 
3143  Out << "namespace {\n";
3144  printType(Out, AT, false, getArrayName(AT), true);
3145  Out << ";\n}\n\n";
3146  }
3147 }
3148 
3149 void CWriter::printContainedArrays(llvm::ArrayType *ATy,
3150  llvm::SmallPtrSet<llvm::Type *, 16> &Printed) {
3151 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
3152  if (!Printed.insert(ATy).second)
3153  return;
3154 #else
3155  if (!Printed.insert(ATy))
3156  return;
3157 #endif
3158 
3159  llvm::ArrayType *ChildTy = llvm::dyn_cast<llvm::ArrayType>(ATy->getElementType());
3160  if (ChildTy != NULL)
3161  printContainedArrays(ChildTy, Printed);
3162 
3163  printType(Out, ATy, false, getArrayName(ATy), true);
3164  Out << ";\n\n";
3165 }
3166 
3167 void CWriter::printFunctionSignature(const llvm::Function *F, bool Prototype) {
3168  /// isStructReturn - Should this function actually return a struct by-value?
3169  bool isStructReturn = F->hasStructRetAttr();
3170 
3171  if (F->hasLocalLinkage()) Out << "static ";
3172 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
3173  if (F->hasDLLImportStorageClass()) Out << "__declspec(dllimport) ";
3174  if (F->hasDLLExportStorageClass()) Out << "__declspec(dllexport) ";
3175 #else
3176  if (F->hasDLLImportLinkage()) Out << "__declspec(dllimport) ";
3177  if (F->hasDLLExportLinkage()) Out << "__declspec(dllexport) ";
3178 #endif
3179  switch (F->getCallingConv()) {
3180  case llvm::CallingConv::X86_StdCall:
3181  Out << "__attribute__((stdcall)) ";
3182  break;
3183  case llvm::CallingConv::X86_FastCall:
3184  Out << "__attribute__((fastcall)) ";
3185  break;
3186  case llvm::CallingConv::X86_ThisCall:
3187  Out << "__attribute__((thiscall)) ";
3188  break;
3189  default:
3190  break;
3191  }
3192 
3193  // Loop over the arguments, printing them...
3194  llvm::FunctionType *FT = llvm::cast<llvm::FunctionType>(F->getFunctionType());
3195 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
3196  const llvm::AttrListPtr &PAL = F->getAttributes();
3197 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
3198  const llvm::AttributeSet &PAL = F->getAttributes();
3199 #else // LLVM 5.0+
3200  const llvm::AttributeList &PAL = F->getAttributes();
3201 #endif
3202 
3203  std::string tstr;
3204  llvm::raw_string_ostream FunctionInnards(tstr);
3205 
3206  // Print out the name...
3207  FunctionInnards << GetValueName(F) << '(';
3208 
3209  bool PrintedArg = false;
3210  if (!F->isDeclaration()) {
3211  if (!F->arg_empty()) {
3212  llvm::Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3213  unsigned Idx = 1;
3214 
3215  // If this is a struct-return function, don't print the hidden
3216  // struct-return argument.
3217  if (isStructReturn) {
3218  assert(I != E && "Invalid struct return function!");
3219  ++I;
3220  ++Idx;
3221  }
3222 
3223  std::string ArgName;
3224  for (; I != E; ++I) {
3225  if (PrintedArg) FunctionInnards << ", ";
3226  if (I->hasName() || !Prototype)
3227 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
3228  ArgName = GetValueName(I);
3229 #else /* LLVM 3.8+ */
3230  ArgName = GetValueName(&*I);
3231 #endif
3232  else
3233  ArgName = "";
3234  llvm::Type *ArgTy = I->getType();
3235 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
3236  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::ByVal)) {
3237 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
3238  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::ByVal)) {
3239 #else // LLVM 5.0+
3240  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::ByVal)) {
3241 #endif
3242  ArgTy = llvm::cast<llvm::PointerType>(ArgTy)->getElementType();
3243 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
3244  ByValParams.insert(I);
3245 #else /* LLVM 3.8+ */
3246  ByValParams.insert(&*I);
3247 #endif
3248  }
3249  printType(FunctionInnards, ArgTy,
3250 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
3251  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::SExt),
3252 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
3253  PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::SExt),
3254 #else // LLVM 5.0+
3255  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::SExt),
3256 #endif
3257  ArgName);
3258  PrintedArg = true;
3259  ++Idx;
3260  }
3261  }
3262  } else {
3263  // Loop over the arguments, printing them.
3264  llvm::FunctionType::param_iterator I = FT->param_begin(), E = FT->param_end();
3265  unsigned Idx = 1;
3266 
3267  // If this is a struct-return function, don't print the hidden
3268  // struct-return argument.
3269  if (isStructReturn) {
3270  assert(I != E && "Invalid struct return function!");
3271  ++I;
3272  ++Idx;
3273  }
3274 
3275  for (; I != E; ++I) {
3276  if (PrintedArg) FunctionInnards << ", ";
3277  llvm::Type *ArgTy = *I;
3278 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
3279  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::ByVal)) {
3280 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
3281  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::ByVal)) {
3282 #else // LLVM 5.0+
3283  if (PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::ByVal)) {
3284 #endif
3285  assert(ArgTy->isPointerTy());
3286  ArgTy = llvm::cast<llvm::PointerType>(ArgTy)->getElementType();
3287  }
3288  printType(FunctionInnards, ArgTy,
3289 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
3290  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attributes::SExt)
3291 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
3292  PAL.getParamAttributes(Idx).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::SExt)
3293 #else // LLVM 5.0+
3294  PAL.getParamAttributes(Idx).hasAttribute(llvm::Attribute::SExt)
3295 #endif
3296  );
3297  PrintedArg = true;
3298  ++Idx;
3299  }
3300  }
3301 
3302  if (!PrintedArg && FT->isVarArg()) {
3303  FunctionInnards << "int vararg_dummy_arg";
3304  PrintedArg = true;
3305  }
3306 
3307  // Finish printing arguments... if this is a vararg function, print the ...,
3308  // unless there are no known types, in which case, we just emit ().
3309  //
3310  if (FT->isVarArg() && PrintedArg) {
3311  FunctionInnards << ",..."; // Output varargs portion of signature!
3312  } else if (!FT->isVarArg() && !PrintedArg) {
3313  FunctionInnards << "void"; // ret() -> ret(void) in C.
3314  }
3315  FunctionInnards << ')';
3316 
3317  // Get the return tpe for the function.
3318  llvm::Type *RetTy;
3319  if (!isStructReturn)
3320  RetTy = F->getReturnType();
3321  else {
3322  // If this is a struct-return function, print the struct-return type.
3323  RetTy = llvm::cast<llvm::PointerType>(FT->getParamType(0))->getElementType();
3324  }
3325 
3326  // Print out the return type and the signature built above.
3327  printType(Out, RetTy,
3328 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
3329  PAL.getParamAttributes(0).hasAttribute(llvm::Attributes::SExt),
3330 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
3331  PAL.getParamAttributes(0).hasAttribute(llvm::AttributeSet::ReturnIndex, llvm::Attribute::SExt),
3332 #else // LLVM 5.0+
3333  PAL.getParamAttributes(0).hasAttribute(llvm::Attribute::SExt),
3334 #endif
3335  FunctionInnards.str());
3336 }
3337 
3338 static inline bool isFPIntBitCast(const llvm::Instruction &I) {
3339  if (!llvm::isa<llvm::BitCastInst>(I))
3340  return false;
3341  llvm::Type *SrcTy = I.getOperand(0)->getType();
3342  llvm::Type *DstTy = I.getType();
3343  return (SrcTy->isFloatingPointTy() && DstTy->isIntegerTy()) ||
3344  (DstTy->isFloatingPointTy() && SrcTy->isIntegerTy());
3345 }
3346 
3347 void CWriter::printFunction(llvm::Function &F) {
3348  /// isStructReturn - Should this function actually return a struct by-value?
3349  bool isStructReturn = F.hasStructRetAttr();
3350 
3351  printFunctionSignature(&F, false);
3352  Out << " {\n";
3353 
3354  // If this is a struct return function, handle the result with magic.
3355  if (isStructReturn) {
3356  llvm::Type *StructTy =
3357  llvm::cast<llvm::PointerType>(F.arg_begin()->getType())->getElementType();
3358  Out << " ";
3359  printType(Out, StructTy, false, "StructReturn");
3360  Out << "; /* Struct return temporary */\n";
3361 
3362  Out << " ";
3363  printType(Out, F.arg_begin()->getType(), false,
3364  GetValueName(&*(F.arg_begin())));
3365  Out << " = &StructReturn;\n";
3366  }
3367 
3368  bool PrintedVar = false;
3369 
3370  // print local variable information for the function
3371  for (llvm::inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
3372  if (const llvm::AllocaInst *AI = isDirectAlloca(&*I)) {
3373  Out << " ";
3374  printType(Out, AI->getAllocatedType(), false, GetValueName(AI));
3375  Out << "; /* Address-exposed local */\n";
3376  PrintedVar = true;
3377  } else if (I->getType() != llvm::Type::getVoidTy(F.getContext()) &&
3378  !isInlinableInst(*I)) {
3379  Out << " ";
3380  printType(Out, I->getType(), false, GetValueName(&*I));
3381  Out << ";\n";
3382 
3383  if (llvm::isa<llvm::PHINode>(*I)) { // Print out PHI node temporaries as well...
3384  Out << " ";
3385  printType(Out, I->getType(), false,
3386  GetValueName(&*I)+"__PHI");
3387  Out << ";\n";
3388  }
3389  PrintedVar = true;
3390  }
3391  // We need a temporary for the BitCast to use so it can pluck a value out
3392  // of a union to do the BitCast. This is separate from the need for a
3393  // variable to hold the result of the BitCast.
3394  if (isFPIntBitCast(*I)) {
3395  Out << " llvmBitCastUnion " << GetValueName(&*I)
3396  << "__BITCAST_TEMPORARY;\n";
3397  PrintedVar = true;
3398  }
3399  }
3400 
3401  if (PrintedVar)
3402  Out << '\n';
3403 
3404  if (F.hasExternalLinkage() && F.getName() == "main")
3405  Out << " CODE_FOR_MAIN();\n";
3406 
3407  // print the basic blocks
3408  for (llvm::Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
3409  if (llvm::Loop *L = LI->getLoopFor(&*BB)) {
3410 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_9 // LLVM 3.9+
3411  if (L->getHeader()->getIterator() == BB && L->getParentLoop() == 0)
3412 #else
3413  if (L->getHeader() == BB && L->getParentLoop() == 0)
3414 #endif
3415  printLoop(L);
3416  } else {
3417  printBasicBlock(&*BB);
3418  }
3419  }
3420 
3421  Out << "}\n\n";
3422 }
3423 
3424 void CWriter::printLoop(llvm::Loop *L) {
3425  Out << " do { /* Syntactic loop '" << L->getHeader()->getName()
3426  << "' to make GCC happy */\n";
3427  for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
3428  llvm::BasicBlock *BB = L->getBlocks()[i];
3429  llvm::Loop *BBLoop = LI->getLoopFor(BB);
3430  if (BBLoop == L)
3431  printBasicBlock(BB);
3432  else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L)
3433  printLoop(BBLoop);
3434  }
3435  Out << " } while (1); /* end of syntactic loop '"
3436  << L->getHeader()->getName() << "' */\n";
3437 }
3438 
3439 void CWriter::printBasicBlock(llvm::BasicBlock *BB) {
3440 
3441  // Don't print the label for the basic block if there are no uses, or if
3442  // the only terminator use is the predecessor basic block's terminator.
3443  // We have to scan the use list because PHI nodes use basic blocks too but
3444  // do not require a label to be generated.
3445  //
3446  bool NeedsLabel = false;
3447  for (llvm::pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
3448  if (isGotoCodeNecessary(*PI, BB)) {
3449  NeedsLabel = true;
3450  break;
3451  }
3452 
3453  if (NeedsLabel) Out << GetValueName(BB) << ": {\n";
3454 
3455  // Output all of the instructions in the basic block...
3456  for (llvm::BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E;
3457  ++II) {
3458  if (!isInlinableInst(*II) && !isDirectAlloca(&*II)) {
3459  if (II->getType() != llvm::Type::getVoidTy(BB->getContext()) &&
3460  !isInlineAsm(*II))
3461  outputLValue(&*II);
3462  else
3463  Out << " ";
3464  writeInstComputationInline(*II);
3465  Out << ";\n";
3466  }
3467  }
3468 
3469  // Don't emit prefix or suffix for the terminator.
3470  visit(*BB->getTerminator());
3471  if (NeedsLabel) Out << "}\n"; // workaround g++ bug
3472 }
3473 
3474 
3475 // Specific Instruction type classes... note that all of the casts are
3476 // necessary because we use the instruction classes as opaque types...
3477 //
3478 void CWriter::visitReturnInst(llvm::ReturnInst &I) {
3479  // If this is a struct return function, return the temporary struct.
3480  bool isStructReturn = I.getParent()->getParent()->hasStructRetAttr();
3481 
3482  if (isStructReturn) {
3483  Out << " return StructReturn;\n";
3484  return;
3485  }
3486 
3487  // Don't output a void return if this is the last basic block in the function
3488  if (I.getNumOperands() == 0 &&
3489  &*--I.getParent()->getParent()->end() == I.getParent() &&
3490  (!I.getParent()->size()) == 1) {
3491  return;
3492  }
3493 
3494  Out << " return";
3495  if (I.getNumOperands()) {
3496  Out << ' ';
3497  writeOperand(I.getOperand(0));
3498  }
3499  Out << ";\n";
3500 }
3501 
3502 void CWriter::visitSwitchInst(llvm::SwitchInst &SI) {
3503 
3504  llvm::Value* Cond = SI.getCondition();
3505 
3506  Out << " switch (";
3507  writeOperand(Cond);
3508  Out << ") {\n default:\n";
3509  printPHICopiesForSuccessor (SI.getParent(), SI.getDefaultDest(), 2);
3510  printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
3511  Out << ";\n";
3512 
3513  for (llvm::SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
3514 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
3515  llvm::ConstantInt* CaseVal = i.getCaseValue();
3516  llvm::BasicBlock* Succ = i.getCaseSuccessor();
3517 #else // LLVM 5.0+
3518  llvm::ConstantInt* CaseVal = i->getCaseValue();
3519  llvm::BasicBlock* Succ = i->getCaseSuccessor();
3520 #endif
3521  Out << " case ";
3522  writeOperand(CaseVal);
3523  Out << ":\n";
3524  printPHICopiesForSuccessor (SI.getParent(), Succ, 2);
3525  printBranchToBlock(SI.getParent(), Succ, 2);
3526 
3527 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
3528  if (llvm::Function::iterator(Succ) == std::next(llvm::Function::iterator(SI.getParent())))
3529 #else
3530  if (llvm::Function::iterator(Succ) == llvm::next(llvm::Function::iterator(SI.getParent())))
3531 #endif
3532  Out << " break;\n";
3533  }
3534 
3535  Out << " }\n";
3536 }
3537 
3538 void CWriter::visitIndirectBrInst(llvm::IndirectBrInst &IBI) {
3539  Out << " goto *(void*)(";
3540  writeOperand(IBI.getOperand(0));
3541  Out << ");\n";
3542 }
3543 
3544 void CWriter::visitUnreachableInst(llvm::UnreachableInst &I) {
3545  Out << " /*UNREACHABLE*/;\n";
3546 }
3547 
3548 bool CWriter::isGotoCodeNecessary(llvm::BasicBlock *From, llvm::BasicBlock *To) {
3549  /// FIXME: This should be reenabled, but loop reordering safe!!
3550  return true;
3551 
3552 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
3553  if (std::next(llvm::Function::iterator(From)) != llvm::Function::iterator(To))
3554 #else
3555  if (llvm::next(llvm::Function::iterator(From)) != llvm::Function::iterator(To))
3556 #endif
3557  return true; // Not the direct successor, we need a goto.
3558 
3559  //llvm::isa<llvm::SwitchInst>(From->getTerminator())
3560 
3561  if (LI->getLoopFor(From) != LI->getLoopFor(To))
3562  return true;
3563  return false;
3564 }
3565 
3566 void CWriter::printPHICopiesForSuccessor (llvm::BasicBlock *CurBlock,
3567  llvm::BasicBlock *Successor,
3568  unsigned Indent) {
3569  for (llvm::BasicBlock::iterator I = Successor->begin(); llvm::isa<llvm::PHINode>(I); ++I) {
3570 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
3571  llvm::PHINode *PN = llvm::cast<llvm::PHINode>(I);
3572 #else /* LLVM 3.8+ */
3573  llvm::PHINode *PN = llvm::cast<llvm::PHINode>(&*I);
3574 #endif
3575  // Now we have to do the printing.
3576  llvm::Value *IV = PN->getIncomingValueForBlock(CurBlock);
3577  if (!llvm::isa<llvm::UndefValue>(IV)) {
3578  Out << std::string(Indent, ' ');
3579 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
3580  Out << " " << GetValueName(I) << "__PHI = ";
3581 #else /* LLVM 3.8+ */
3582  Out << " " << GetValueName(&*I) << "__PHI = ";
3583 #endif
3584  writeOperand(IV);
3585  Out << "; /* for PHI node */\n";
3586  }
3587  }
3588 }
3589 
3590 void CWriter::printBranchToBlock(llvm::BasicBlock *CurBB, llvm::BasicBlock *Succ,
3591  unsigned Indent) {
3592  if (isGotoCodeNecessary(CurBB, Succ)) {
3593  Out << std::string(Indent, ' ') << " goto ";
3594  writeOperand(Succ);
3595  Out << ";\n";
3596  }
3597 }
3598 
3599 // Branch instruction printing - Avoid printing out a branch to a basic block
3600 // that immediately succeeds the current one.
3601 //
3602 void CWriter::visitBranchInst(llvm::BranchInst &I) {
3603 
3604  if (I.isConditional()) {
3605  if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) {
3606  Out << " if (";
3607  writeOperand(I.getCondition());
3608  Out << ") {\n";
3609 
3610  printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2);
3611  printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
3612 
3613  if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) {
3614  Out << " } else {\n";
3615  printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
3616  printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
3617  }
3618  } else {
3619  // First goto not necessary, assume second one is...
3620  Out << " if (!";
3621  writeOperand(I.getCondition());
3622  Out << ") {\n";
3623 
3624  printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
3625  printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
3626  }
3627 
3628  Out << " }\n";
3629  } else {
3630  printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0);
3631  printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
3632  }
3633  Out << "\n";
3634 }
3635 
3636 // PHI nodes get copied into temporary values at the end of predecessor basic
3637 // blocks. We now need to copy these temporary values into the REAL value for
3638 // the PHI.
3639 void CWriter::visitPHINode(llvm::PHINode &I) {
3640  writeOperand(&I);
3641  Out << "__PHI";
3642 }
3643 
3644 
3645 void CWriter::visitBinaryOperator(llvm::Instruction &I) {
3646  // binary instructions, shift instructions, setCond instructions.
3647  assert(!I.getType()->isPointerTy());
3648 
3649  if (llvm::isa<const llvm::VectorType>(I.getOperand(0)->getType())) {
3650  const char *intrinsic = NULL;
3651  switch (I.getOpcode()) {
3652  case llvm::Instruction::Add: intrinsic = "__add"; break;
3653  case llvm::Instruction::FAdd: intrinsic = "__add"; break;
3654  case llvm::Instruction::Sub: intrinsic = "__sub"; break;
3655  case llvm::Instruction::FSub: intrinsic = "__sub"; break;
3656  case llvm::Instruction::Mul: intrinsic = "__mul"; break;
3657  case llvm::Instruction::FMul: intrinsic = "__mul"; break;
3658  case llvm::Instruction::URem: intrinsic = "__urem"; break;
3659  case llvm::Instruction::SRem: intrinsic = "__srem"; break;
3660  case llvm::Instruction::FRem: intrinsic = "__frem"; break;
3661  case llvm::Instruction::UDiv: intrinsic = "__udiv"; break;
3662  case llvm::Instruction::SDiv: intrinsic = "__sdiv"; break;
3663  case llvm::Instruction::FDiv: intrinsic = "__div"; break;
3664  case llvm::Instruction::And: intrinsic = "__and"; break;
3665  case llvm::Instruction::Or: intrinsic = "__or"; break;
3666  case llvm::Instruction::Xor: intrinsic = "__xor"; break;
3667  case llvm::Instruction::Shl : intrinsic = "__shl"; break;
3668  case llvm::Instruction::LShr: intrinsic = "__lshr"; break;
3669  case llvm::Instruction::AShr: intrinsic = "__ashr"; break;
3670  default:
3671 #ifndef NDEBUG
3672  llvm::errs() << "Invalid operator type!" << I;
3673 #endif
3674  llvm_unreachable(0);
3675  }
3676  Out << intrinsic;
3677  Out << "(";
3678  writeOperand(I.getOperand(0));
3679  Out << ", ";
3680  if ((I.getOpcode() == llvm::Instruction::Shl ||
3681  I.getOpcode() == llvm::Instruction::LShr ||
3682  I.getOpcode() == llvm::Instruction::AShr)) {
3683  llvm::Value *splat = NULL;
3684  if (LLVMVectorValuesAllEqual(I.getOperand(1), &splat)) {
3685  if (splat) {
3686  // Avoid __extract_element(splat(value), 0), if possible.
3687  writeOperand(splat);
3688  } else {
3689  Out << "__extract_element(";
3690  writeOperand(I.getOperand(1));
3691  Out << ", 0) ";
3692  }
3693  }
3694  else
3695  writeOperand(I.getOperand(1));
3696  }
3697  else
3698  writeOperand(I.getOperand(1));
3699  Out << ")";
3700  return;
3701  }
3702 
3703  // We must cast the results of binary operations which might be promoted.
3704  bool needsCast = false;
3705  if ((I.getType() == llvm::Type::getInt8Ty(I.getContext())) ||
3706  (I.getType() == llvm::Type::getInt16Ty(I.getContext()))
3707  || (I.getType() == llvm::Type::getFloatTy(I.getContext()))) {
3708  needsCast = true;
3709  Out << "((";
3710  printType(Out, I.getType(), false);
3711  Out << ")(";
3712  }
3713 
3714  // If this is a negation operation, print it out as such. For FP, we don't
3715  // want to print "-0.0 - X".
3716 #if ISPC_LLVM_VERSION > ISPC_LLVM_7_0 // LLVM 8.0+
3717  llvm::Value *X;
3718  if (match(&I, m_Neg(llvm::PatternMatch::m_Value(X)))) {
3719  Out << "-(";
3720  writeOperand(X);
3721  Out << ")";
3722  }
3723  else if (match(&I, m_FNeg(llvm::PatternMatch::m_Value(X)))) {
3724  Out << "-(";
3725  writeOperand(X);
3726  Out << ")";
3727  }
3728 #else
3729  if (llvm::BinaryOperator::isNeg(&I)) {
3730  Out << "-(";
3731  writeOperand(llvm::BinaryOperator::getNegArgument(llvm::cast<llvm::BinaryOperator>(&I)));
3732  Out << ")";
3733  }
3734  else if (llvm::BinaryOperator::isFNeg(&I)) {
3735  Out << "-(";
3736  writeOperand(llvm::BinaryOperator::getFNegArgument(llvm::cast<llvm::BinaryOperator>(&I)));
3737  Out << ")";
3738  }
3739 #endif
3740  else if (I.getOpcode() == llvm::Instruction::FRem) {
3741  // Output a call to fmod/fmodf instead of emitting a%b
3742  if (I.getType() == llvm::Type::getFloatTy(I.getContext()))
3743  Out << "fmodf(";
3744  else if (I.getType() == llvm::Type::getDoubleTy(I.getContext()))
3745  Out << "fmod(";
3746  else // all 3 flavors of long double
3747  Out << "fmodl(";
3748  writeOperand(I.getOperand(0));
3749  Out << ", ";
3750  writeOperand(I.getOperand(1));
3751  Out << ")";
3752  } else {
3753 
3754  // Write out the cast of the instruction's value back to the proper type
3755  // if necessary.
3756  bool NeedsClosingParens = writeInstructionCast(I);
3757 
3758  // Certain instructions require the operand to be forced to a specific type
3759  // so we use writeOperandWithCast here instead of writeOperand. Similarly
3760  // below for operand 1
3761  writeOperandWithCast(I.getOperand(0), I.getOpcode());
3762 
3763  switch (I.getOpcode()) {
3764  case llvm::Instruction::Add:
3765  case llvm::Instruction::FAdd: Out << " + "; break;
3766  case llvm::Instruction::Sub:
3767  case llvm::Instruction::FSub: Out << " - "; break;
3768  case llvm::Instruction::Mul:
3769  case llvm::Instruction::FMul: Out << " * "; break;
3770  case llvm::Instruction::URem:
3771  case llvm::Instruction::SRem:
3772  case llvm::Instruction::FRem: Out << " % "; break;
3773  case llvm::Instruction::UDiv:
3774  case llvm::Instruction::SDiv:
3775  case llvm::Instruction::FDiv: Out << " / "; break;
3776  case llvm::Instruction::And: Out << " & "; break;
3777  case llvm::Instruction::Or: Out << " | "; break;
3778  case llvm::Instruction::Xor: Out << " ^ "; break;
3779  case llvm::Instruction::Shl : Out << " << "; break;
3780  case llvm::Instruction::LShr:
3781  case llvm::Instruction::AShr: Out << " >> "; break;
3782  default:
3783 #ifndef NDEBUG
3784  llvm::errs() << "Invalid operator type!" << I;
3785 #endif
3786  llvm_unreachable(0);
3787  }
3788 
3789  writeOperandWithCast(I.getOperand(1), I.getOpcode());
3790  if (NeedsClosingParens)
3791  Out << "))";
3792  }
3793 
3794  if (needsCast) {
3795  Out << "))";
3796  }
3797 }
3798 
3799 
3800 static const char *
3801 lPredicateToString(llvm::CmpInst::Predicate p) {
3802  switch (p) {
3803  case llvm::ICmpInst::ICMP_EQ: return "__equal";
3804  case llvm::ICmpInst::ICMP_NE: return "__not_equal";
3805  case llvm::ICmpInst::ICMP_ULE: return "__unsigned_less_equal";
3806  case llvm::ICmpInst::ICMP_SLE: return "__signed_less_equal";
3807  case llvm::ICmpInst::ICMP_UGE: return "__unsigned_greater_equal";
3808  case llvm::ICmpInst::ICMP_SGE: return "__signed_greater_equal";
3809  case llvm::ICmpInst::ICMP_ULT: return "__unsigned_less_than";
3810  case llvm::ICmpInst::ICMP_SLT: return "__signed_less_than";
3811  case llvm::ICmpInst::ICMP_UGT: return "__unsigned_greater_than";
3812  case llvm::ICmpInst::ICMP_SGT: return "__signed_greater_than";
3813 
3814  case llvm::FCmpInst::FCMP_ORD: return "__ordered";
3815  case llvm::FCmpInst::FCMP_UNO: return "__unordered";
3816  case llvm::FCmpInst::FCMP_UEQ: return "__equal";
3817  case llvm::FCmpInst::FCMP_UNE: return "__not_equal";
3818  case llvm::FCmpInst::FCMP_ULT: return "__less_than";
3819  case llvm::FCmpInst::FCMP_ULE: return "__less_equal";
3820  case llvm::FCmpInst::FCMP_UGT: return "__greater_than";
3821  case llvm::FCmpInst::FCMP_UGE: return "__greater_equal";
3822  case llvm::FCmpInst::FCMP_OEQ: return "__equal";
3823  case llvm::FCmpInst::FCMP_ONE: return "__not_equal";
3824  case llvm::FCmpInst::FCMP_OLT: return "__less_than";
3825  case llvm::FCmpInst::FCMP_OLE: return "__less_equal";
3826  case llvm::FCmpInst::FCMP_OGT: return "__greater_than";
3827  case llvm::FCmpInst::FCMP_OGE: return "__greater_equal";
3828 
3829  default: llvm_unreachable(0); return NULL;
3830  }
3831 }
3832 
3833 
3834 static const char *
3835 lTypeToSuffix(llvm::Type *t) {
3836  llvm::VectorType *vt = llvm::dyn_cast<llvm::VectorType>(t);
3837  Assert(vt != NULL);
3838  t = vt->getElementType();
3839 
3840  switch (t->getTypeID()) {
3841  case llvm::Type::FloatTyID: return "float";
3842  case llvm::Type::DoubleTyID: return "double";
3843  case llvm::Type::IntegerTyID: {
3844  switch (llvm::cast<llvm::IntegerType>(t)->getBitWidth()) {
3845  case 1: return "i1";
3846  case 8: return "i8";
3847  case 16: return "i16";
3848  case 32: return "i32";
3849  case 64: return "i64";
3850  }
3851  }
3852  default: llvm_unreachable(0); return NULL;
3853  }
3854  return NULL;
3855 }
3856 
3857 
3858 void CWriter::visitICmpInst(llvm::ICmpInst &I) {
3859  bool isVector = llvm::isa<llvm::VectorType>(I.getOperand(0)->getType());
3860 
3861  if (isVector) {
3862  Out << lPredicateToString(I.getPredicate());
3863  Out << "_";
3864  Out << lTypeToSuffix(I.getOperand(0)->getType());
3865  Out << "(";
3866  writeOperand(I.getOperand(0));
3867  Out << ", ";
3868  writeOperand(I.getOperand(1));
3869  Out << ")";
3870  return;
3871  }
3872 
3873  // Write out the cast of the instruction's value back to the proper type
3874  // if necessary.
3875  bool NeedsClosingParens = writeInstructionCast(I);
3876 
3877  // Certain icmp predicate require the operand to be forced to a specific type
3878  // so we use writeOperandWithCast here instead of writeOperand. Similarly
3879  // below for operand 1
3880  writeOperandWithCast(I.getOperand(0), I);
3881 
3882  switch (I.getPredicate()) {
3883  case llvm::ICmpInst::ICMP_EQ: Out << " == "; break;
3884  case llvm::ICmpInst::ICMP_NE: Out << " != "; break;
3885  case llvm::ICmpInst::ICMP_ULE:
3886  case llvm::ICmpInst::ICMP_SLE: Out << " <= "; break;
3887  case llvm::ICmpInst::ICMP_UGE:
3888  case llvm::ICmpInst::ICMP_SGE: Out << " >= "; break;
3889  case llvm::ICmpInst::ICMP_ULT:
3890  case llvm::ICmpInst::ICMP_SLT: Out << " < "; break;
3891  case llvm::ICmpInst::ICMP_UGT:
3892  case llvm::ICmpInst::ICMP_SGT: Out << " > "; break;
3893  default:
3894 #ifndef NDEBUG
3895  llvm::errs() << "Invalid icmp predicate!" << I;
3896 #endif
3897  llvm_unreachable(0);
3898  }
3899 
3900  writeOperandWithCast(I.getOperand(1), I);
3901  if (NeedsClosingParens)
3902  Out << "))";
3903 }
3904 
3905 void CWriter::visitFCmpInst(llvm::FCmpInst &I) {
3906  bool isVector = llvm::isa<llvm::VectorType>(I.getOperand(0)->getType());
3907 
3908  if (I.getPredicate() == llvm::FCmpInst::FCMP_FALSE) {
3909  if (isVector)
3910  llvm::report_fatal_error("FIXME: vector FCMP_FALSE");
3911  else
3912  Out << "0";
3913  return;
3914  }
3915  if (I.getPredicate() == llvm::FCmpInst::FCMP_TRUE) {
3916  if (isVector)
3917  llvm::report_fatal_error("FIXME: vector FCMP_TRUE");
3918  else
3919  Out << "1";
3920  return;
3921  }
3922 
3923  if (isVector) {
3924  Out << lPredicateToString(I.getPredicate());
3925  Out << "_";
3926  Out << lTypeToSuffix(I.getOperand(0)->getType());
3927  Out << "(";
3928  }
3929  else {
3930  const char* op = 0;
3931  switch (I.getPredicate()) {
3932  default: llvm_unreachable("Illegal FCmp predicate");
3933  case llvm::FCmpInst::FCMP_ORD: op = "ord"; break;
3934  case llvm::FCmpInst::FCMP_UNO: op = "uno"; break;
3935 
3936  case llvm::FCmpInst::FCMP_UEQ: op = "ueq"; break;
3937  case llvm::FCmpInst::FCMP_UNE: op = "une"; break;
3938  case llvm::FCmpInst::FCMP_ULT: op = "ult"; break;
3939  case llvm::FCmpInst::FCMP_ULE: op = "ule"; break;
3940  case llvm::FCmpInst::FCMP_UGT: op = "ugt"; break;
3941  case llvm::FCmpInst::FCMP_UGE: op = "uge"; break;
3942 
3943  case llvm::FCmpInst::FCMP_OEQ: op = "oeq"; break;
3944  case llvm::FCmpInst::FCMP_ONE: op = "one"; break;
3945  case llvm::FCmpInst::FCMP_OLT: op = "olt"; break;
3946  case llvm::FCmpInst::FCMP_OLE: op = "ole"; break;
3947  case llvm::FCmpInst::FCMP_OGT: op = "ogt"; break;
3948  case llvm::FCmpInst::FCMP_OGE: op = "oge"; break;
3949  }
3950 
3951  Out << "llvm_fcmp_" << op << "(";
3952  }
3953 
3954  // Write the first operand
3955  writeOperand(I.getOperand(0));
3956  Out << ", ";
3957  // Write the second operand
3958  writeOperand(I.getOperand(1));
3959  Out << ")";
3960 }
3961 
3962 static const char * getFloatBitCastField(llvm::Type *Ty) {
3963  switch (Ty->getTypeID()) {
3964  default: llvm_unreachable("Invalid Type");
3965  case llvm::Type::FloatTyID: return "Float";
3966  case llvm::Type::DoubleTyID: return "Double";
3967  case llvm::Type::IntegerTyID: {
3968  unsigned NumBits = llvm::cast<llvm::IntegerType>(Ty)->getBitWidth();
3969  if (NumBits <= 32)
3970  return "Int32";
3971  else
3972  return "Int64";
3973  }
3974  }
3975 }
3976 
3977 void CWriter::visitCastInst(llvm::CastInst &I) {
3978  llvm::Type *DstTy = I.getType();
3979  llvm::Type *SrcTy = I.getOperand(0)->getType();
3980  if (isFPIntBitCast(I)) {
3981  Out << '(';
3982  // These int<->float and long<->double casts need to be handled specially
3983  Out << GetValueName(&I) << "__BITCAST_TEMPORARY."
3984  << getFloatBitCastField(I.getOperand(0)->getType()) << " = ";
3985  writeOperand(I.getOperand(0));
3986  Out << ", " << GetValueName(&I) << "__BITCAST_TEMPORARY."
3987  << getFloatBitCastField(I.getType());
3988  Out << ')';
3989  return;
3990  }
3991 
3992  if ((llvm::isa<llvm::VectorType>(DstTy)) && (!llvm::isa<llvm::VectorType>(SrcTy))) {
3993  writeOperand(I.getOperand(0));
3994  return;
3995  }
3996 
3997  Out << '(';
3998  bool closeParen = printCast(I.getOpcode(), SrcTy, DstTy);
3999 
4000  // Make a sext from i1 work by subtracting the i1 from 0 (an int).
4001  if (SrcTy == llvm::Type::getInt1Ty(I.getContext()) &&
4002  I.getOpcode() == llvm::Instruction::SExt)
4003  Out << "0-";
4004 
4005  writeOperand(I.getOperand(0));
4006 
4007  if (DstTy == llvm::Type::getInt1Ty(I.getContext()) &&
4008  (I.getOpcode() == llvm::Instruction::Trunc ||
4009  I.getOpcode() == llvm::Instruction::FPToUI ||
4010  I.getOpcode() == llvm::Instruction::FPToSI ||
4011  I.getOpcode() == llvm::Instruction::PtrToInt)) {
4012  // Make sure we really get a trunc to bool by anding the operand with 1
4013  Out << "&1u";
4014  }
4015  Out << ')';
4016  if (closeParen)
4017  Out << ')';
4018 }
4019 
4020 void CWriter::visitSelectInst(llvm::SelectInst &I) {
4021  if (llvm::isa<llvm::VectorType>(I.getType())) {
4022  Out << "__select(";
4023  writeOperand(I.getCondition());
4024  Out << ", ";
4025  writeOperand(I.getTrueValue());
4026  Out << ", ";
4027  writeOperand(I.getFalseValue());
4028  Out << ")";
4029  return;
4030  }
4031 
4032  Out << "((";
4033  writeOperand(I.getCondition());
4034  Out << ") ? (";
4035  writeOperand(I.getTrueValue());
4036  Out << ") : (";
4037  writeOperand(I.getFalseValue());
4038  Out << "))";
4039 }
4040 
4041 // Returns the macro name or value of the max or min of an integer type
4042 // (as defined in limits.h).
4043 static void printLimitValue(llvm::IntegerType &Ty, bool isSigned, bool isMax,
4044  llvm::raw_ostream &Out) {
4045  const char* type;
4046  const char* sprefix = "";
4047 
4048  unsigned NumBits = Ty.getBitWidth();
4049  if (NumBits <= 8) {
4050  type = "CHAR";
4051  sprefix = "S";
4052  } else if (NumBits <= 16) {
4053  type = "SHRT";
4054  } else if (NumBits <= 32) {
4055  type = "INT";
4056  } else if (NumBits <= 64) {
4057  type = "LLONG";
4058  } else {
4059  llvm_unreachable("Bit widths > 64 not implemented yet");
4060  }
4061 
4062  if (isSigned)
4063  Out << sprefix << type << (isMax ? "_MAX" : "_MIN");
4064  else
4065  Out << "U" << type << (isMax ? "_MAX" : "0");
4066 }
4067 
4068 #ifndef NDEBUG
4069 static bool isSupportedIntegerSize(llvm::IntegerType &T) {
4070  return T.getBitWidth() == 8 || T.getBitWidth() == 16 ||
4071  T.getBitWidth() == 32 || T.getBitWidth() == 64;
4072 }
4073 #endif
4074 
4075 void CWriter::printIntrinsicDefinition(const llvm::Function &F, llvm::raw_ostream &Out) {
4076  llvm::FunctionType *funT = F.getFunctionType();
4077  llvm::Type *retT = F.getReturnType();
4078  llvm::IntegerType *elemT = llvm::cast<llvm::IntegerType>(funT->getParamType(1));
4079 
4080  assert(isSupportedIntegerSize(*elemT) &&
4081  "CBackend does not support arbitrary size integers.");
4082  assert(llvm::cast<llvm::StructType>(retT)->getElementType(0) == elemT &&
4083  elemT == funT->getParamType(0) && funT->getNumParams() == 2);
4084 
4085  switch (F.getIntrinsicID()) {
4086  default:
4087  llvm_unreachable("Unsupported Intrinsic.");
4088  case llvm::Intrinsic::uadd_with_overflow:
4089  // static inline Rty uadd_ixx(unsigned ixx a, unsigned ixx b) {
4090  // Rty r;
4091  // r.field0 = a + b;
4092  // r.field1 = (r.field0 < a);
4093  // return r;
4094  // }
4095  Out << "static inline ";
4096  printType(Out, retT);
4097  Out << GetValueName(&F);
4098  Out << "(";
4099  printSimpleType(Out, elemT, false);
4100  Out << "a,";
4101  printSimpleType(Out, elemT, false);
4102  Out << "b) {\n ";
4103  printType(Out, retT);
4104  Out << "r;\n";
4105  Out << " r.field0 = a + b;\n";
4106  Out << " r.field1 = (r.field0 < a);\n";
4107  Out << " return r;\n}\n";
4108  break;
4109 
4110  case llvm::Intrinsic::sadd_with_overflow:
4111  // static inline Rty sadd_ixx(ixx a, ixx b) {
4112  // Rty r;
4113  // r.field1 = (b > 0 && a > XX_MAX - b) ||
4114  // (b < 0 && a < XX_MIN - b);
4115  // r.field0 = r.field1 ? 0 : a + b;
4116  // return r;
4117  // }
4118  Out << "static ";
4119  printType(Out, retT);
4120  Out << GetValueName(&F);
4121  Out << "(";
4122  printSimpleType(Out, elemT, true);
4123  Out << "a,";
4124  printSimpleType(Out, elemT, true);
4125  Out << "b) {\n ";
4126  printType(Out, retT);
4127  Out << "r;\n";
4128  Out << " r.field1 = (b > 0 && a > ";
4129  printLimitValue(*elemT, true, true, Out);
4130  Out << " - b) || (b < 0 && a < ";
4131  printLimitValue(*elemT, true, false, Out);
4132  Out << " - b);\n";
4133  Out << " r.field0 = r.field1 ? 0 : a + b;\n";
4134  Out << " return r;\n}\n";
4135  break;
4136 
4137  case llvm::Intrinsic::umul_with_overflow:
4138  Out << "static inline ";
4139  printType(Out, retT);
4140  Out << GetValueName(&F);
4141  Out << "(";
4142  printSimpleType(Out, elemT, false);
4143  Out << "a,";
4144  printSimpleType(Out, elemT, false);
4145  Out << "b) {\n ";
4146 
4147  printType(Out, retT);
4148  Out << "r;\n";
4149 
4150  unsigned NumBits = llvm::cast<llvm::IntegerType>(elemT)->getBitWidth();
4151  std::stringstream str_type;
4152  if (NumBits <= 32)
4153  str_type << "uint" << 2 * NumBits << "_t";
4154  else {
4155  assert(NumBits <= 64 && "Bit widths > 128 not implemented yet");
4156  str_type << "llvmUInt128";
4157  }
4158 
4159  Out << " " << str_type.str() << " result = (" << str_type.str() << ") a * (" << str_type.str() << ") b;\n";
4160  Out << " r.field0 = result;\n";
4161  Out << " r.field1 = result >> " << NumBits << ";\n";
4162  Out << " return r;\n}\n";
4163  break;
4164  }
4165 }
4166 
4167 void CWriter::lowerIntrinsics(llvm::Function &F) {
4168  // This is used to keep track of intrinsics that get generated to a lowered
4169  // function. We must generate the prototypes before the function body which
4170  // will only be expanded on first use (by the loop below).
4171  std::vector<llvm::Function*> prototypesToGen;
4172 
4173  // Examine all the instructions in this function to find the intrinsics that
4174  // need to be lowered.
4175  for (llvm::Function::iterator BB = F.begin(), EE = F.end(); BB != EE; ++BB)
4176  for (llvm::BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
4177  if (llvm::CallInst *CI = llvm::dyn_cast<llvm::CallInst>(I++))
4178  if (llvm::Function *F = CI->getCalledFunction())
4179  switch (F->getIntrinsicID()) {
4180  // We directly implement these intrinsics
4181  case llvm::Intrinsic::not_intrinsic:
4182  case llvm::Intrinsic::vastart:
4183  case llvm::Intrinsic::vacopy:
4184  case llvm::Intrinsic::vaend:
4185  case llvm::Intrinsic::returnaddress:
4186  case llvm::Intrinsic::frameaddress:
4187  case llvm::Intrinsic::setjmp:
4188  case llvm::Intrinsic::longjmp:
4189  case llvm::Intrinsic::memset:
4190  case llvm::Intrinsic::prefetch:
4191  case llvm::Intrinsic::powi:
4192  case llvm::Intrinsic::fabs:
4193  case llvm::Intrinsic::x86_sse_cmp_ss:
4194  case llvm::Intrinsic::x86_sse_cmp_ps:
4195  case llvm::Intrinsic::x86_sse2_cmp_sd:
4196  case llvm::Intrinsic::x86_sse2_cmp_pd:
4197  case llvm::Intrinsic::ppc_altivec_lvsl:
4198  case llvm::Intrinsic::uadd_with_overflow:
4199  case llvm::Intrinsic::sadd_with_overflow:
4200  case llvm::Intrinsic::trap:
4201  case llvm::Intrinsic::objectsize:
4202  case llvm::Intrinsic::readcyclecounter:
4203  case llvm::Intrinsic::umul_with_overflow:
4204  // Or we just ignore them because of their uselessness in C++ source
4205  case llvm::Intrinsic::dbg_value:
4206  case llvm::Intrinsic::dbg_declare:
4207  break;
4208  default:
4209  // If this is an intrinsic that directly corresponds to a GCC
4210  // builtin, we handle it.
4211  const char *BuiltinName = "";
4212 #define GET_GCC_BUILTIN_NAME
4213 #define Intrinsic llvm::Intrinsic
4214 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
4215  #include "llvm/Intrinsics.gen"
4216 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_6_0 /* LLVM 3.3-6.0 */
4217  #include "llvm/IR/Intrinsics.gen"
4218 #else /* LLVM 7.0+ */
4219 // This looks completely broken, even in 3.2, need to figure out what's going on here
4220 // and how to fix it (if needed).
4221 // #include "llvm/IR/Intrinsics.inc"
4222 #endif
4223 #undef Intrinsic
4224 #undef GET_GCC_BUILTIN_NAME
4225  // If we handle it, don't lower it.
4226  if (BuiltinName[0]) break;
4227 
4228  // All other intrinsic calls we must lower.
4229  llvm::Instruction *Before = 0;
4230  if (CI != &BB->front())
4231 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
4232  Before = &*std::prev(llvm::BasicBlock::iterator(CI));
4233 #else
4234  Before = prior(llvm::BasicBlock::iterator(CI));
4235 #endif
4236 
4237  IL->LowerIntrinsicCall(CI);
4238  if (Before) { // Move iterator to instruction after call
4239 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
4240  I = Before; ++I;
4241 #else /* LLVM 3.8+ */
4242  I = Before->getIterator(); ++I;
4243 #endif
4244  } else {
4245  I = BB->begin();
4246  }
4247  // If the intrinsic got lowered to another call, and that call has
4248  // a definition then we need to make sure its prototype is emitted
4249  // before any calls to it.
4250  if (llvm::CallInst *Call = llvm::dyn_cast<llvm::CallInst>(I))
4251  if (llvm::Function *NewF = Call->getCalledFunction())
4252  if (!NewF->isDeclaration())
4253  prototypesToGen.push_back(NewF);
4254 
4255  break;
4256  }
4257 
4258  // We may have collected some prototypes to emit in the loop above.
4259  // Emit them now, before the function that uses them is emitted. But,
4260  // be careful not to emit them twice.
4261  std::vector<llvm::Function*>::iterator I = prototypesToGen.begin();
4262  std::vector<llvm::Function*>::iterator E = prototypesToGen.end();
4263  for ( ; I != E; ++I) {
4264  if (intrinsicPrototypesAlreadyGenerated.insert(*I).second) {
4265  Out << '\n';
4266  printFunctionSignature(*I, true);
4267  Out << ";\n";
4268  }
4269  }
4270 }
4271 
4272 void CWriter::visitCallInst(llvm::CallInst &I) {
4273  if (llvm::isa<llvm::InlineAsm>(I.getCalledValue()))
4274  return visitInlineAsm(I);
4275 
4276  bool WroteCallee = false;
4277 
4278  // Handle intrinsic function calls first...
4279  if (llvm::Function *F = I.getCalledFunction())
4280  if (llvm::Intrinsic::ID ID = (llvm::Intrinsic::ID)F->getIntrinsicID())
4281  if (visitBuiltinCall(I, ID, WroteCallee))
4282  return;
4283 
4284  llvm::Value *Callee = I.getCalledValue();
4285 
4286  llvm::PointerType *PTy = llvm::cast<llvm::PointerType>(Callee->getType());
4287  llvm::FunctionType *FTy = llvm::cast<llvm::FunctionType>(PTy->getElementType());
4288 
4289  // If this is a call to a struct-return function, assign to the first
4290  // parameter instead of passing it to the call.
4291 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
4292  const llvm::AttrListPtr &PAL = I.getAttributes();
4293 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
4294  const llvm::AttributeSet &PAL = I.getAttributes();
4295 #else // LLVM 5.0+
4296  const llvm::AttributeList &PAL = I.getAttributes();
4297 #endif
4298 
4299  bool hasByVal = I.hasByValArgument();
4300  bool isStructRet = (I.getNumArgOperands() > 0) && I.hasStructRetAttr();
4301  if (isStructRet) {
4302  writeOperandDeref(I.getArgOperand(0));
4303  Out << " = ";
4304  }
4305 
4306  if (I.isTailCall()) Out << " /*tail*/ ";
4307 
4308  if (!WroteCallee) {
4309  // If this is an indirect call to a struct return function, we need to cast
4310  // the pointer. Ditto for indirect calls with byval arguments.
4311  bool NeedsCast = (hasByVal || isStructRet) && !llvm::isa<llvm::Function>(Callee);
4312 
4313  // GCC is a real PITA. It does not permit codegening casts of functions to
4314  // function pointers if they are in a call (it generates a trap instruction
4315  // instead!). We work around this by inserting a cast to void* in between
4316  // the function and the function pointer cast. Unfortunately, we can't just
4317  // form the constant expression here, because the folder will immediately
4318  // nuke it.
4319  //
4320  // Note finally, that this is completely unsafe. ANSI C does not guarantee
4321  // that void* and function pointers have the same size. :( To deal with this
4322  // in the common case, we handle casts where the number of arguments passed
4323  // match exactly.
4324  //
4325  if (llvm::ConstantExpr *CE = llvm::dyn_cast<llvm::ConstantExpr>(Callee))
4326  if (CE->isCast())
4327  if (llvm::Function *RF = llvm::dyn_cast<llvm::Function>(CE->getOperand(0))) {
4328  NeedsCast = true;
4329  Callee = RF;
4330  }
4331 
4332  if (Callee->getName() == "malloc" ||
4333  Callee->getName() == "_aligned_malloc")
4334  Out << "(uint8_t *)";
4335 
4336  // This 'if' will fix 'soa-18.ispc' test (fails with optimizations off)
4337  // Yet the way the case is fixed is quite dirty and leads to many other fails
4338 
4339  //if (Callee->getName() == "__masked_store_i64") {
4340  // llvm::CallSite CS(&I);
4341  // llvm::CallSite::arg_iterator AI = CS.arg_begin();
4342  // if (is_vec16_i64_ty(llvm::cast<llvm::PointerType>((*AI)->getType())->getElementType())) {
4343  // Out << "/* Replacing store of vec16_i64 val into &vec16_i64 pointer with a simple copy */\n";
4344  // // If we are trying to get a pointer to from a vec16_i64 var
4345  // // It would be better to replace this instruction with a masked copy
4346  // if (llvm::isa<llvm::GetElementPtrInst>(*AI)) {
4347  // writeOperandDeref(*AI);
4348  // Out << " = __select(";
4349  // writeOperand(*(AI+2));
4350  // Out << ", ";
4351  // writeOperand(*(AI+1));
4352  // Out << ", ";
4353  // writeOperandDeref(*AI);
4354  // Out << ")";
4355  // return;
4356  // }
4357  // }
4358  //}
4359 
4360  if (NeedsCast) {
4361  // Ok, just cast the pointer type.
4362  Out << "((";
4363  if (isStructRet)
4364  printStructReturnPointerFunctionType(Out, PAL,
4365  llvm::cast<llvm::PointerType>(I.getCalledValue()->getType()));
4366  else if (hasByVal)
4367  printType(Out, I.getCalledValue()->getType(), false, "", true, PAL);
4368  else
4369  printType(Out, I.getCalledValue()->getType());
4370  Out << ")(void*)";
4371  }
4372  writeOperand(Callee);
4373  if (NeedsCast) Out << ')';
4374  }
4375 
4376  Out << '(';
4377 
4378  bool PrintedArg = false;
4379  if(FTy->isVarArg() && !FTy->getNumParams()) {
4380  Out << "0 /*dummy arg*/";
4381  PrintedArg = true;
4382  }
4383 
4384  unsigned NumDeclaredParams = FTy->getNumParams();
4385  llvm::CallSite CS(&I);
4386  llvm::CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
4387  unsigned ArgNo = 0;
4388  if (isStructRet) { // Skip struct return argument.
4389  ++AI;
4390  ++ArgNo;
4391  }
4392 
4393 
4394  for (; AI != AE; ++AI, ++ArgNo) {
4395  if (PrintedArg) Out << ", ";
4396  if (ArgNo == 0 &&
4397  Callee->getName() == "posix_memalign") {
4398  // uint8_t** is incompatible with void** without explicit cast.
4399  // Should be do this any other functions?
4400  Out << "(void **)";
4401  }
4402  else if (ArgNo < NumDeclaredParams &&
4403  (*AI)->getType() != FTy->getParamType(ArgNo)) {
4404  Out << '(';
4405  printType(Out, FTy->getParamType(ArgNo),
4406 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
4407  PAL.getParamAttributes(ArgNo+1).hasAttribute(llvm::Attributes::SExt)
4408 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
4409  PAL.getParamAttributes(ArgNo+1).hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::SExt)
4410 #else // LLVM 5.0+
4411  PAL.getParamAttributes(ArgNo+1).hasAttribute(llvm::Attribute::SExt)
4412 #endif
4413  );
4414  Out << ')';
4415  }
4416  // Check if the argument is expected to be passed by value.
4417 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
4418  if (I.paramHasAttr(ArgNo+1,
4419 #else // LLVM 5.0+
4420  if (I.paramHasAttr(ArgNo,
4421 #endif
4422 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
4423  llvm::Attributes::ByVal
4424 #else /* LLVM 3.3+ */
4425  llvm::Attribute::ByVal
4426 #endif
4427  )) {
4428  writeOperandDeref(*AI);
4429  }
4430  else {
4431  writeOperand(*AI);
4432  }
4433  PrintedArg = true;
4434  }
4435  Out << ')';
4436 }
4437 
4438 /// visitBuiltinCall - Handle the call to the specified builtin. Returns true
4439 /// if the entire call is handled, return false if it wasn't handled, and
4440 /// optionally set 'WroteCallee' if the callee has already been printed out.
4441 bool CWriter::visitBuiltinCall(llvm::CallInst &I, llvm::Intrinsic::ID ID,
4442  bool &WroteCallee) {
4443  switch (ID) {
4444  default: {
4445  // If this is an intrinsic that directly corresponds to a GCC
4446  // builtin, we emit it here.
4447  const char *BuiltinName = "";
4448 #define GET_GCC_BUILTIN_NAME
4449 #define Intrinsic llvm::Intrinsic
4450 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
4451  #include "llvm/Intrinsics.gen"
4452 #elif ISPC_LLVM_VERSION <= ISPC_LLVM_6_0 /* LLVM 3.3-6.0 */
4453  #include "llvm/IR/Intrinsics.gen"
4454 #else /* LLVM 7.0+ */
4455 // This looks completely broken, even in 3.2, need to figure out what's going on here
4456 // and how to fix it (if needed).
4457 // #include "llvm/IR/Intrinsics.inc"
4458 #endif
4459 #undef Intrinsic
4460 #undef GET_GCC_BUILTIN_NAME
4461  assert(BuiltinName[0] && "Unknown LLVM intrinsic!");
4462 
4463  Out << BuiltinName;
4464  WroteCallee = true;
4465  return false;
4466  }
4467  // Ignoring debug intrinsics
4468  case llvm::Intrinsic::dbg_value:
4469  case llvm::Intrinsic::dbg_declare:
4470  return true;
4471  case llvm::Intrinsic::vastart:
4472  Out << "0; ";
4473 
4474  Out << "va_start(*(va_list*)";
4475  writeOperand(I.getArgOperand(0));
4476  Out << ", ";
4477  // Output the last argument to the enclosing function.
4478  if (I.getParent()->getParent()->arg_empty())
4479  Out << "vararg_dummy_arg";
4480  else
4481  writeOperand(&*(std::prev(I.getParent()->getParent()->arg_end())));
4482  Out << ')';
4483  return true;
4484  case llvm::Intrinsic::vaend:
4485  if (!llvm::isa<llvm::ConstantPointerNull>(I.getArgOperand(0))) {
4486  Out << "0; va_end(*(va_list*)";
4487  writeOperand(I.getArgOperand(0));
4488  Out << ')';
4489  } else {
4490  Out << "va_end(*(va_list*)0)";
4491  }
4492  return true;
4493  case llvm::Intrinsic::vacopy:
4494  Out << "0; ";
4495  Out << "va_copy(*(va_list*)";
4496  writeOperand(I.getArgOperand(0));
4497  Out << ", *(va_list*)";
4498  writeOperand(I.getArgOperand(1));
4499  Out << ')';
4500  return true;
4501  case llvm::Intrinsic::returnaddress:
4502  Out << "__builtin_return_address(";
4503  writeOperand(I.getArgOperand(0));
4504  Out << ')';
4505  return true;
4506  case llvm::Intrinsic::frameaddress:
4507  Out << "__builtin_frame_address(";
4508  writeOperand(I.getArgOperand(0));
4509  Out << ')';
4510  return true;
4511  case llvm::Intrinsic::powi:
4512  Out << "__builtin_powi(";
4513  writeOperand(I.getArgOperand(0));
4514  Out << ", ";
4515  writeOperand(I.getArgOperand(1));
4516  Out << ')';
4517  return true;
4518  case llvm::Intrinsic::fabs:
4519  Out << "__builtin_fabs(";
4520  writeOperand(I.getArgOperand(0));
4521  Out << ')';
4522  return true;
4523  case llvm::Intrinsic::setjmp:
4524  Out << "setjmp(*(jmp_buf*)";
4525  writeOperand(I.getArgOperand(0));
4526  Out << ')';
4527  return true;
4528  case llvm::Intrinsic::longjmp:
4529  Out << "longjmp(*(jmp_buf*)";
4530  writeOperand(I.getArgOperand(0));
4531  Out << ", ";
4532  writeOperand(I.getArgOperand(1));
4533  Out << ')';
4534  return true;
4535  case llvm::Intrinsic::memset:
4536  Out << "Memset(";
4537  writeOperand(I.getArgOperand(0));
4538  Out << ", ";
4539  writeOperand(I.getArgOperand(1));
4540  Out << ", ";
4541  writeOperand(I.getArgOperand(2));
4542  Out << ')';
4543  return true;
4544  case llvm::Intrinsic::prefetch:
4545  Out << "LLVM_PREFETCH((const void *)";
4546  writeOperand(I.getArgOperand(0));
4547  Out << ", ";
4548  writeOperand(I.getArgOperand(1));
4549  Out << ", ";
4550  writeOperand(I.getArgOperand(2));
4551  Out << ")";
4552  return true;
4553  case llvm::Intrinsic::stacksave:
4554  // Emit this as: Val = 0; *((void**)&Val) = __builtin_stack_save()
4555  // to work around GCC bugs (see PR1809).
4556  Out << "0; *((void**)&" << GetValueName(&I)
4557  << ") = __builtin_stack_save()";
4558  return true;
4559  case llvm::Intrinsic::x86_sse_cmp_ss:
4560  case llvm::Intrinsic::x86_sse_cmp_ps:
4561  case llvm::Intrinsic::x86_sse2_cmp_sd:
4562  case llvm::Intrinsic::x86_sse2_cmp_pd:
4563  Out << '(';
4564  printType(Out, I.getType());
4565  Out << ')';
4566  // Multiple GCC builtins multiplex onto this intrinsic.
4567  switch (llvm::cast<llvm::ConstantInt>(I.getArgOperand(2))->getZExtValue()) {
4568  default: llvm_unreachable("Invalid llvm.x86.sse.cmp!");
4569  case 0: Out << "__builtin_ia32_cmpeq"; break;
4570  case 1: Out << "__builtin_ia32_cmplt"; break;
4571  case 2: Out << "__builtin_ia32_cmple"; break;
4572  case 3: Out << "__builtin_ia32_cmpunord"; break;
4573  case 4: Out << "__builtin_ia32_cmpneq"; break;
4574  case 5: Out << "__builtin_ia32_cmpnlt"; break;
4575  case 6: Out << "__builtin_ia32_cmpnle"; break;
4576  case 7: Out << "__builtin_ia32_cmpord"; break;
4577  }
4578  if (ID == llvm::Intrinsic::x86_sse_cmp_ps || ID == llvm::Intrinsic::x86_sse2_cmp_pd)
4579  Out << 'p';
4580  else
4581  Out << 's';
4582  if (ID == llvm::Intrinsic::x86_sse_cmp_ss || ID == llvm::Intrinsic::x86_sse_cmp_ps)
4583  Out << 's';
4584  else
4585  Out << 'd';
4586 
4587  Out << "(";
4588  writeOperand(I.getArgOperand(0));
4589  Out << ", ";
4590  writeOperand(I.getArgOperand(1));
4591  Out << ")";
4592  return true;
4593  case llvm::Intrinsic::ppc_altivec_lvsl:
4594  Out << '(';
4595  printType(Out, I.getType());
4596  Out << ')';
4597  Out << "__builtin_altivec_lvsl(0, (void*)";
4598  writeOperand(I.getArgOperand(0));
4599  Out << ")";
4600  return true;
4601  case llvm::Intrinsic::uadd_with_overflow:
4602  case llvm::Intrinsic::sadd_with_overflow:
4603  case llvm::Intrinsic::umul_with_overflow:
4604  Out << GetValueName(I.getCalledFunction()) << "(";
4605  writeOperand(I.getArgOperand(0));
4606  Out << ", ";
4607  writeOperand(I.getArgOperand(1));
4608  Out << ")";
4609  return true;
4610  case llvm::Intrinsic::trap:
4611  Out << "abort()";
4612  return true;
4613  case llvm::Intrinsic::objectsize:
4614  return true;
4615  case llvm::Intrinsic::readcyclecounter:
4616  Out << "__clock()";
4617  return true;
4618  }
4619 }
4620 
4621 
4622 //TODO: assumptions about what consume arguments from the call are likely wrong
4623 // handle communitivity
4624 void CWriter::visitInlineAsm(llvm::CallInst &CI) {
4625  assert(!"Inline assembly not supported");
4626 }
4627 
4628 void CWriter::visitAllocaInst(llvm::AllocaInst &I) {
4629  Out << '(';
4630  printType(Out, I.getType());
4631  Out << ") alloca(sizeof(";
4632  printType(Out, I.getType()->getElementType());
4633  Out << ')';
4634  if (I.isArrayAllocation()) {
4635  Out << " * " ;
4636  writeOperand(I.getOperand(0));
4637  }
4638  Out << ')';
4639 }
4640 
4641 void CWriter::printGEPExpression(llvm::Value *Ptr, llvm::gep_type_iterator I,
4642  llvm::gep_type_iterator E, bool Static) {
4643 
4644  // If there are no indices, just print out the pointer.
4645  if (I == E) {
4646  writeOperand(Ptr);
4647  return;
4648  }
4649 
4650  // Find out if the last index is into a vector. If so, we have to print this
4651  // specially. Since vectors can't have elements of indexable type, only the
4652  // last index could possibly be of a vector element.
4653  llvm::VectorType *LastIndexIsVector = 0;
4654  {
4655  for (llvm::gep_type_iterator TmpI = I; TmpI != E; ++TmpI)
4656 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9
4657  LastIndexIsVector = llvm::dyn_cast<llvm::VectorType>(*TmpI);
4658 #else // LLVM 4.0+
4659  LastIndexIsVector = llvm::dyn_cast<llvm::VectorType>(TmpI.getIndexedType());
4660 #endif
4661  }
4662 
4663  Out << "(";
4664 
4665  // If the last index is into a vector, we can't print it as &a[i][j] because
4666  // we can't index into a vector with j in GCC. Instead, emit this as
4667  // (((float*)&a[i])+j)
4668  if (LastIndexIsVector) {
4669  Out << "((";
4670  printType(Out, llvm::PointerType::getUnqual(LastIndexIsVector->getElementType()));
4671  Out << ")(";
4672  }
4673 
4674  Out << '&';
4675 
4676  llvm::Type *ParentTy = Ptr->getType();
4677 
4678  // If the first index is 0 (very typical) we can do a number of
4679  // simplifications to clean up the code.
4680  llvm::Value *FirstOp = I.getOperand();
4681  if (!llvm::isa<llvm::Constant>(FirstOp) || !llvm::cast<llvm::Constant>(FirstOp)->isNullValue()) {
4682  // First index isn't simple, print it the hard way.
4683  writeOperand(Ptr);
4684  } else {
4685  ParentTy = I.getIndexedType(); // Skip the zero index.
4686  ++I;
4687 
4688  // Okay, emit the first operand. If Ptr is something that is already address
4689  // exposed, like a global, avoid emitting (&foo)[0], just emit foo instead.
4690  if (isAddressExposed(Ptr)) {
4691  writeOperandInternal(Ptr, Static);
4692 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9
4693  } else if (I != E && (*I)->isStructTy()) {
4694 #else // LLVM 4.0+
4695  } else if (I != E && I.isStruct()) {
4696 #endif
4697  // If we didn't already emit the first operand, see if we can print it as
4698  // P->f instead of "P[0].f"
4699  writeOperand(Ptr);
4700  Out << "->field" << llvm::cast<llvm::ConstantInt>(I.getOperand())->getZExtValue();
4701  ParentTy = I.getIndexedType();
4702  ++I; // eat the struct index as well.
4703  } else {
4704  // Instead of emitting P[0][1], emit (*P)[1], which is more idiomatic.
4705  Out << "(*";
4706  writeOperand(Ptr);
4707  Out << ")";
4708  }
4709  }
4710 
4711  for (; I != E; ++I) {
4712  if (ParentTy->isStructTy()) {
4713  Out << ".field" << llvm::cast<llvm::ConstantInt>(I.getOperand())->getZExtValue();
4714  } else if (ParentTy->isArrayTy()) {
4715  Out << ".array[";
4716  writeOperandWithCast(I.getOperand(), llvm::Instruction::GetElementPtr);
4717  Out << ']';
4718  } else if (!ParentTy->isVectorTy()) {
4719  Out << '[';
4720  writeOperandWithCast(I.getOperand(), llvm::Instruction::GetElementPtr);
4721  Out << ']';
4722  } else {
4723  // If the last index is into a vector, then print it out as "+j)". This
4724  // works with the 'LastIndexIsVector' code above.
4725  if (llvm::isa<llvm::Constant>(I.getOperand()) &&
4726  llvm::cast<llvm::Constant>(I.getOperand())->isNullValue()) {
4727  Out << "))"; // avoid "+0".
4728  } else {
4729  Out << ")+(";
4730  writeOperandWithCast(I.getOperand(), llvm::Instruction::GetElementPtr);
4731  Out << "))";
4732  }
4733  }
4734  ParentTy = I.getIndexedType();
4735  }
4736  Out << ")";
4737 }
4738 
4739 void CWriter::writeMemoryAccess(llvm::Value *Operand, llvm::Type *OperandType,
4740  bool IsVolatile, unsigned Alignment) {
4741  assert(!llvm::isa<llvm::VectorType>(OperandType));
4742  bool IsUnaligned = Alignment &&
4743  Alignment < TD->getABITypeAlignment(OperandType);
4744 
4745  llvm::IntegerType *ITy = llvm::dyn_cast<llvm::IntegerType>(OperandType);
4746  if (!IsUnaligned)
4747  Out << '*';
4748  if (IsVolatile || IsUnaligned) {
4749  Out << "((";
4750  if (IsUnaligned && ITy && (ITy->getBitWidth() > 64))
4751  Out << "iN_" << ITy->getBitWidth() << "_align_" << Alignment << " *)";
4752  else {
4753  if (IsUnaligned)
4754  Out << "struct __attribute__ ((packed, aligned(" << Alignment << "))) {";
4755  printType(Out, OperandType, false, IsUnaligned ? "data" : "volatile*");
4756  if (IsUnaligned) {
4757  Out << "; } ";
4758  if (IsVolatile) Out << "volatile ";
4759  Out << "*";
4760  }
4761  Out << ")";
4762  }
4763  }
4764 
4765  writeOperand(Operand);
4766 
4767  if (IsVolatile || IsUnaligned) {
4768  Out << ')';
4769  if (IsUnaligned)
4770  Out << "->data";
4771  }
4772 }
4773 
4774 void CWriter::visitLoadInst(llvm::LoadInst &I) {
4775  llvm::VectorType *VT = llvm::dyn_cast<llvm::VectorType>(I.getType());
4776  if (VT != NULL) {
4777  Out << "__load<" << I.getAlignment() << ">(";
4778  writeOperand(I.getOperand(0));
4779  Out << ")";
4780  return;
4781  }
4782 
4783  writeMemoryAccess(I.getOperand(0), I.getType(), I.isVolatile(),
4784  I.getAlignment());
4785 }
4786 
4787 void CWriter::visitStoreInst(llvm::StoreInst &I) {
4788  llvm::VectorType *VT = llvm::dyn_cast<llvm::VectorType>(I.getOperand(0)->getType());
4789  if (VT != NULL) {
4790  Out << "__store<" << I.getAlignment() << ">(";
4791  writeOperand(I.getOperand(1));
4792  Out << ", ";
4793  writeOperand(I.getOperand(0));
4794  Out << ")";
4795  return;
4796  }
4797 
4798  writeMemoryAccess(I.getPointerOperand(), I.getOperand(0)->getType(),
4799  I.isVolatile(), I.getAlignment());
4800  Out << " = ";
4801  llvm::Value *Operand = I.getOperand(0);
4802  llvm::Constant *BitMask = 0;
4803  if (llvm::IntegerType* ITy = llvm::dyn_cast<llvm::IntegerType>(Operand->getType()))
4804  if (!ITy->isPowerOf2ByteWidth())
4805  // We have a bit width that doesn't match an even power-of-2 byte
4806  // size. Consequently we must & the value with the type's bit mask
4807  BitMask = llvm::ConstantInt::get(ITy, ITy->getBitMask());
4808  if (BitMask)
4809  Out << "((";
4810  writeOperand(Operand);
4811  if (BitMask) {
4812  Out << ") & ";
4813  printConstant(BitMask, false);
4814  Out << ")";
4815  }
4816 }
4817 
4818 void CWriter::visitGetElementPtrInst(llvm::GetElementPtrInst &I) {
4819  printGEPExpression(I.getPointerOperand(), gep_type_begin(I),
4820  gep_type_end(I), false);
4821 }
4822 
4823 void CWriter::visitVAArgInst(llvm::VAArgInst &I) {
4824  Out << "va_arg(*(va_list*)";
4825  writeOperand(I.getOperand(0));
4826  Out << ", ";
4827  printType(Out, I.getType());
4828  Out << ");\n ";
4829 }
4830 
4831 void CWriter::visitInsertElementInst(llvm::InsertElementInst &I) {
4832 #if 0
4833  Type *EltTy = I.getType()->getElementType();
4834  writeOperand(I.getOperand(0));
4835  Out << ";\n ";
4836  Out << "((";
4837  printType(Out, llvm::PointerType::getUnqual(EltTy));
4838  Out << ")(&" << GetValueName(&I) << "))[";
4839  writeOperand(I.getOperand(2));
4840  Out << "] = (";
4841  writeOperand(I.getOperand(1));
4842  Out << ")";
4843 #else
4844  writeOperand(I.getOperand(0));
4845  Out << ";\n ";
4846  Out << "__insert_element(&" << GetValueName(&I) << ", ";
4847  writeOperand(I.getOperand(2));
4848  Out << ", ";
4849  writeOperand(I.getOperand(1));
4850  Out << ")";
4851 #endif
4852 }
4853 
4854 void CWriter::visitExtractElementInst(llvm::ExtractElementInst &I) {
4855  // We know that our operand is not inlined.
4856 #if 0
4857  Out << "((";
4858  Type *EltTy =
4859  llvm::cast<llvm::VectorType>(I.getOperand(0)->getType())->getElementType();
4860  printType(Out, llvm::PointerType::getUnqual(EltTy));
4861  Out << ")(&" << GetValueName(I.getOperand(0)) << "))[";
4862  writeOperand(I.getOperand(1));
4863  Out << "]";
4864 #else
4865  Out << "(__extract_element(";
4866  writeOperand(I.getOperand(0));
4867  Out << ", ";
4868  writeOperand(I.getOperand(1));
4869  Out << "))";
4870 #endif
4871 }
4872 
4873 void CWriter::visitShuffleVectorInst(llvm::ShuffleVectorInst &SVI) {
4874  printType(Out, SVI.getType());
4875  Out << "(";
4876  llvm::VectorType *VT = SVI.getType();
4877  unsigned NumElts = VT->getNumElements();
4878  llvm::Type *EltTy = VT->getElementType();
4879  llvm::VectorType *OpTy = llvm::dyn_cast<llvm::VectorType>(SVI.getOperand(0)->getType());
4880  unsigned OpElts = OpTy->getNumElements();
4881 
4882  for (unsigned i = 0; i != NumElts; ++i) {
4883  if (i) Out << ", ";
4884  int SrcVal = SVI.getMaskValue(i);
4885  if ((unsigned)SrcVal >= 2*OpElts) {
4886  Out << " 0/*undef*/ ";
4887  } else {
4888  llvm::Value *Op = SVI.getOperand((unsigned)SrcVal >= OpElts);
4889  SrcVal &= OpElts - 1;
4890 
4891  if (llvm::isa<llvm::ConstantVector>(Op)) {
4892  printConstant(llvm::cast<llvm::ConstantVector>(Op)->getOperand(SrcVal),
4893  false);
4894  } else if (llvm::isa<llvm::ConstantAggregateZero>(Op) || llvm::isa<llvm::UndefValue>(Op)) {
4895  Out << "0";
4896  }
4897  else {
4898  // Do an extractelement of this value from the appropriate input.
4899  Out << " \n#if defined(KNC) \n";
4900  if (OpElts != 1) { // all __vec16_* have overloaded operator []
4901  Out << "(" << GetValueName(Op)
4902  << ")[" << SrcVal << "]";
4903  }
4904  else { // but __vec1_* don't have it
4905  Out << "((";
4906  printType(Out, llvm::PointerType::getUnqual(EltTy));
4907  Out << ")(&" << GetValueName(Op)
4908  << "))[" << SrcVal << "]";
4909  }
4910  Out << " \n#else \n";
4911  Out << "((";
4912  printType(Out, llvm::PointerType::getUnqual(EltTy));
4913  Out << ")(&" << GetValueName(Op)
4914  << "))[" << SrcVal << "]";
4915  Out << " \n#endif \n";
4916  }
4917  }
4918  }
4919  Out << ")";
4920 }
4921 
4922 void CWriter::visitInsertValueInst(llvm::InsertValueInst &IVI) {
4923  // Start by copying the entire aggregate value into the result variable.
4924  writeOperand(IVI.getOperand(0));
4925  Out << ";\n ";
4926 
4927  // Then do the insert to update the field.
4928  Out << GetValueName(&IVI);
4929  for (const unsigned *b = IVI.idx_begin(), *i = b, *e = IVI.idx_end();
4930  i != e; ++i) {
4931  llvm::Type *IndexedTy = (b == i) ? IVI.getOperand(0)->getType() :
4932  llvm::ExtractValueInst::getIndexedType(IVI.getOperand(0)->getType(),
4933  llvm::makeArrayRef(b, i));
4934  if (IndexedTy->isArrayTy())
4935  Out << ".array[" << *i << "]";
4936  else
4937  Out << ".field" << *i;
4938  }
4939  Out << " = ";
4940  writeOperand(IVI.getOperand(1));
4941 }
4942 
4943 void CWriter::visitExtractValueInst(llvm::ExtractValueInst &EVI) {
4944  Out << "(";
4945  if (llvm::isa<llvm::UndefValue>(EVI.getOperand(0))) {
4946  // FIXME: need to handle these--a 0 initializer won't do...
4947  assert(!llvm::isa<llvm::VectorType>(EVI.getType()));
4948  Out << "(";
4949  printType(Out, EVI.getType());
4950  Out << ") 0/*UNDEF*/";
4951  } else {
4952  Out << GetValueName(EVI.getOperand(0));
4953  for (const unsigned *b = EVI.idx_begin(), *i = b, *e = EVI.idx_end();
4954  i != e; ++i) {
4955  llvm::Type *IndexedTy = (b == i) ? EVI.getOperand(0)->getType() :
4956  llvm::ExtractValueInst::getIndexedType(EVI.getOperand(0)->getType(),
4957  llvm::makeArrayRef(b, i));
4958  if (IndexedTy->isArrayTy())
4959  Out << ".array[" << *i << "]";
4960  else
4961  Out << ".field" << *i;
4962  }
4963  }
4964  Out << ")";
4965 }
4966 
4967 void CWriter::visitAtomicRMWInst(llvm::AtomicRMWInst &AI) {
4968  Out << "(";
4969  Out << "__atomic_";
4970  switch (AI.getOperation()) {
4971  default: llvm_unreachable("Unhandled case in visitAtomicRMWInst!");
4972  case llvm::AtomicRMWInst::Add: Out << "add"; break;
4973  case llvm::AtomicRMWInst::Sub: Out << "sub"; break;
4974  case llvm::AtomicRMWInst::Xchg: Out << "xchg"; break;
4975  case llvm::AtomicRMWInst::And: Out << "and"; break;
4976  case llvm::AtomicRMWInst::Nand: Out << "nand"; break;
4977  case llvm::AtomicRMWInst::Or: Out << "or"; break;
4978  case llvm::AtomicRMWInst::Xor: Out << "xor"; break;
4979  case llvm::AtomicRMWInst::Min: Out << "min"; break;
4980  case llvm::AtomicRMWInst::Max: Out << "max"; break;
4981  case llvm::AtomicRMWInst::UMin: Out << "umin"; break;
4982  case llvm::AtomicRMWInst::UMax: Out << "umax"; break;
4983  }
4984  Out << "(";
4985  writeOperand(AI.getOperand(0));
4986  Out << ", ";
4987  writeOperand(AI.getOperand(1));
4988  Out << "))";
4989 }
4990 
4991 void CWriter::visitAtomicCmpXchgInst(llvm::AtomicCmpXchgInst &ACXI) {
4992  Out << "(";
4993 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
4994  printType(Out, ACXI.getType(), false);
4995  Out << "::init("; // LLVM cmpxchg returns a struct, so we need make an assighment properly
4996 #endif
4997  Out << "__atomic_cmpxchg(";
4998  writeOperand(ACXI.getPointerOperand());
4999  Out << ", ";
5000  writeOperand(ACXI.getCompareOperand());
5001  Out << ", ";
5002  writeOperand(ACXI.getNewValOperand());
5003  Out << ")";
5004 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
5005  Out << ", true /* There is no way to learn the value of this bit inside ISPC, so making it constant */)";
5006 #endif
5007  Out << ")";
5008 }
5009 
5010 ///////////////////////////////////////////////////////////////////////////
5011 // SmearCleanupPass
5012 
5013 class SmearCleanupPass : public llvm::BasicBlockPass {
5014 public:
5015  SmearCleanupPass(llvm::Module *m, int width)
5016  : BasicBlockPass(ID) { module = m; vectorWidth = width; }
5017 
5018 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9 // <= 3.9
5019  const char *getPassName() const { return "Smear Cleanup Pass"; }
5020 #else // LLVM 4.0+
5021  llvm::StringRef getPassName() const { return "Smear Cleanup Pass"; }
5022 #endif
5023  bool runOnBasicBlock(llvm::BasicBlock &BB);
5024 
5025  static char ID;
5026  llvm::Module *module;
5027  unsigned int vectorWidth;
5028 
5029 private:
5030  unsigned int ChainLength(llvm::InsertElementInst *inst) const;
5031  llvm::Value *getInsertChainSmearValue(llvm::Instruction* inst) const;
5032  llvm::Value *getShuffleSmearValue(llvm::Instruction* inst) const;
5033 };
5034 
5035 
5036 char SmearCleanupPass::ID = 0;
5037 
5038 
5039 unsigned int
5040 SmearCleanupPass::ChainLength(llvm::InsertElementInst *inst) const {
5041  unsigned int length = 0;
5042  while (inst != NULL) {
5043  ++length;
5044  inst = llvm::dyn_cast<llvm::InsertElementInst>(inst->getOperand(0));
5045  }
5046  return length;
5047 }
5048 
5049 
5050 llvm::Value *
5051 SmearCleanupPass::getInsertChainSmearValue(llvm::Instruction* inst) const {
5052  // TODO: we don't check indexes where we do insertion, so we may trigger
5053  // transformation for a wrong chain.
5054  // This way of doing broadcast is obsolete and should be probably removed
5055  // some day.
5056 
5057  llvm::InsertElementInst *insertInst =
5058  llvm::dyn_cast<llvm::InsertElementInst>(inst);
5059  if (!insertInst) {
5060  return NULL;
5061  }
5062 
5063  // We consider only chians of vectorWidth length.
5064  if (ChainLength(insertInst) != vectorWidth) {
5065  return NULL;
5066  }
5067 
5068  // FIXME: we only want to do this to vectors with width equal to
5069  // the target vector width. But we can't easily get that here, so
5070  // for now we at least avoid one case where we definitely don't
5071  // want to do this.
5072  llvm::VectorType *vt = llvm::dyn_cast<llvm::VectorType>(insertInst->getType());
5073  if (vt->getNumElements() == 1) {
5074  return NULL;
5075  }
5076 
5077  llvm::Value *smearValue = NULL;
5078  while (insertInst != NULL) {
5079  // operand 1 is inserted value
5080  llvm::Value *insertValue = insertInst->getOperand(1);
5081  if (smearValue == NULL) {
5082  smearValue = insertValue;
5083  }
5084  else if (smearValue != insertValue) {
5085  return NULL;
5086  }
5087 
5088  // operand 0 is a vector to insert into.
5089  insertInst =
5090  llvm::dyn_cast<llvm::InsertElementInst>(insertInst->getOperand(0));
5091  }
5092  assert(smearValue != NULL);
5093 
5094  return smearValue;
5095 }
5096 
5097 
5098 llvm::Value *
5099 SmearCleanupPass::getShuffleSmearValue(llvm::Instruction* inst) const {
5100  llvm::ShuffleVectorInst *shuffleInst =
5101  llvm::dyn_cast<llvm::ShuffleVectorInst>(inst);
5102  if (!shuffleInst) {
5103  return NULL;
5104  }
5105 
5106  llvm::Constant* mask =
5107  llvm::dyn_cast<llvm::Constant>(shuffleInst->getOperand(2));
5108 
5109  // Check that the shuffle is a broadcast of the element of the first vector,
5110  // i.e. mask vector is vector with equal elements of expected size.
5111  if (!(mask &&
5112 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
5113  (mask->isNullValue() || (shuffleInst->getMask()->getType()->isVectorTy() && llvm::dyn_cast<llvm::ConstantVector>(shuffleInst->getMask())->getSplatValue() != 0 ) ) &&
5114 #else
5115  (mask->isNullValue() || (shuffleInst->getMask()->getSplatValue() != 0))&&
5116 #endif
5117  llvm::dyn_cast<llvm::VectorType>(mask->getType())->getNumElements() == vectorWidth)) {
5118  return NULL;
5119  }
5120 
5121 
5122  llvm::InsertElementInst *insertInst =
5123  llvm::dyn_cast<llvm::InsertElementInst>(shuffleInst->getOperand(0));
5124 
5125  // Check that it's an InsertElementInst that inserts a value to first element.
5126  if (!(insertInst &&
5127  llvm::isa<llvm::Constant>(insertInst->getOperand(2)) &&
5128  llvm::dyn_cast<llvm::Constant>(insertInst->getOperand(2))->isNullValue())) {
5129 
5130  // We can't extract element from vec1
5131  llvm::VectorType *operandVec = llvm::dyn_cast<llvm::VectorType>(shuffleInst->getOperand(0)->getType());
5132  if (operandVec && operandVec->getNumElements() == 1)
5133  return NULL;
5134 
5135  // Insert ExtractElementInstr to get value for smear
5136 
5137  llvm::Function *extractFunc = module->getFunction("__extract_element");
5138 
5139  if (extractFunc == NULL) {
5140  // Declare the __extract_element function if needed; it takes a vector and
5141  // a scalar parameter and returns a scalar of the vector parameter type.
5142  llvm::Constant *ef =
5143 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
5144  module->getOrInsertFunction("__extract_element",
5145  shuffleInst->getOperand(0)->getType()->getVectorElementType(),
5146  shuffleInst->getOperand(0)->getType(),
5147  llvm::IntegerType::get(module->getContext(), 32), NULL);
5148 #else // LLVM 5.0+
5149  module->getOrInsertFunction("__extract_element",
5150  shuffleInst->getOperand(0)->getType()->getVectorElementType(),
5151  shuffleInst->getOperand(0)->getType(),
5152  llvm::IntegerType::get(module->getContext(), 32));
5153 #endif
5154  extractFunc = llvm::dyn_cast<llvm::Function>(ef);
5155  assert(extractFunc != NULL);
5156  extractFunc->setDoesNotThrow();
5157  extractFunc->setOnlyReadsMemory();
5158  }
5159 
5160  if (extractFunc == NULL) {
5161  return NULL;
5162  }
5163  llvm::Instruction *extractCall =
5164  llvm::ExtractElementInst::Create(shuffleInst->getOperand(0),
5165 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
5166  // mask is of VectorType
5167  llvm::dyn_cast<llvm::ConstantVector>(mask)->getSplatValue(),
5168 #else
5169  mask->getSplatValue(),
5170 #endif
5171  "__extract_element", inst);
5172  return extractCall;
5173  }
5174 
5175  llvm::Value *result = insertInst->getOperand(1);
5176 
5177  return result;
5178 }
5179 
5180 
5181 bool
5182 SmearCleanupPass::runOnBasicBlock(llvm::BasicBlock &bb) {
5183  bool modifiedAny = false;
5184 
5185  restart:
5186  for (llvm::BasicBlock::iterator iter = bb.begin(), e = bb.end(); iter != e; ++iter) {
5187  llvm::Value *smearValue = NULL;
5188 
5189  if (!(smearValue = getInsertChainSmearValue(&*iter)) &&
5190  !(smearValue = getShuffleSmearValue(&*iter))) {
5191  continue;
5192  }
5193 
5194  llvm::Type *smearType = smearValue->getType();
5195  const char *smearFuncName = lGetTypedFunc("smear", smearType, vectorWidth);
5196  if (smearFuncName != NULL) {
5197  llvm::Function *smearFunc = module->getFunction(smearFuncName);
5198  if (smearFunc == NULL) {
5199  // Declare the smear function if needed; it takes a single
5200  // scalar parameter and returns a vector of the same
5201  // parameter type.
5202  llvm::Constant *sf =
5203 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
5204  module->getOrInsertFunction(smearFuncName, iter->getType(),
5205  smearType, NULL);
5206 #else // LLVM 5.0+
5207  module->getOrInsertFunction(smearFuncName, iter->getType(),
5208  smearType);
5209 #endif
5210  smearFunc = llvm::dyn_cast<llvm::Function>(sf);
5211  assert(smearFunc != NULL);
5212  smearFunc->setDoesNotThrow();
5213  smearFunc->setDoesNotAccessMemory();
5214  }
5215 
5216  assert(smearFunc != NULL);
5217  llvm::Value *args[1] = { smearValue };
5218  llvm::ArrayRef<llvm::Value *> argArray(&args[0], &args[1]);
5219  llvm::Instruction *smearCall =
5220  llvm::CallInst::Create(smearFunc, argArray, LLVMGetName(smearValue, "_smear"),
5221  (llvm::Instruction *)NULL);
5222 
5223  ReplaceInstWithInst(&*iter, smearCall);
5224 
5225  modifiedAny = true;
5226  goto restart;
5227  }
5228  }
5229 
5230  return modifiedAny;
5231 }
5232 
5233 
5234 ///////////////////////////////////////////////////////////////////////////
5235 // AndCmpCleanupPass
5236 
5237 class AndCmpCleanupPass : public llvm::BasicBlockPass {
5238 public:
5239  AndCmpCleanupPass()
5240  : BasicBlockPass(ID) { }
5241 
5242 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9 // <= 3.9
5243  const char *getPassName() const { return "AndCmp Cleanup Pass"; }
5244 #else // LLVM 4.0+
5245  llvm::StringRef getPassName() const { return "AndCmp Cleanup Pass"; }
5246 #endif
5247  bool runOnBasicBlock(llvm::BasicBlock &BB);
5248 
5249  static char ID;
5250 };
5251 
5252 char AndCmpCleanupPass::ID = 0;
5253 
5254 // Look for ANDs of masks where one of the operands is a vector compare; we
5255 // can turn these into specialized calls to masked vector compares and
5256 // thence eliminate the AND. For example, rather than emitting
5257 // __and(__less(a, b), c), we will emit __less_and_mask(a, b, c).
5258 bool
5259 AndCmpCleanupPass::runOnBasicBlock(llvm::BasicBlock &bb) {
5260  bool modifiedAny = false;
5261 
5262  restart:
5263  for (llvm::BasicBlock::iterator iter = bb.begin(), e = bb.end(); iter != e; ++iter) {
5264  // See if we have an AND instruction
5265  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(&*iter);
5266  if (bop == NULL || bop->getOpcode() != llvm::Instruction::And)
5267  continue;
5268 
5269  // Make sure it's a vector AND
5270  if (llvm::isa<llvm::VectorType>(bop->getType()) == false)
5271  continue;
5272 
5273  // We only care about ANDs of the mask type, not, e.g. ANDs of
5274  // int32s vectors.
5275  if (bop->getType() != LLVMTypes::MaskType)
5276  continue;
5277 
5278  // Now see if either of the operands to the AND is a comparison
5279  for (int i = 0; i < 2; ++i) {
5280  llvm::Value *op = bop->getOperand(i);
5281  llvm::CmpInst *opCmp = llvm::dyn_cast<llvm::CmpInst>(op);
5282  if (opCmp == NULL)
5283  continue;
5284 
5285  // We have a comparison. However, we also need to make sure
5286  // that it's not comparing two mask values; those can't be
5287  // simplified to something simpler.
5288  if (opCmp->getOperand(0)->getType() == LLVMTypes::MaskType)
5289  break;
5290 
5291  // Success! Go ahead and replace the AND with a call to the
5292  // "__and_mask" variant of the comparison function for this
5293  // operand.
5294  std::string funcName = lPredicateToString(opCmp->getPredicate());
5295  funcName += "_";
5296  funcName += lTypeToSuffix(opCmp->getOperand(0)->getType());
5297  funcName += "_and_mask";
5298 
5299  llvm::Function *andCmpFunc = m->module->getFunction(funcName);
5300  if (andCmpFunc == NULL) {
5301  // Declare the function if needed; the first two arguments
5302  // are the same as the two arguments to the compare we're
5303  // replacing and the third argument is the mask type.
5304  llvm::Type *cmpOpType = opCmp->getOperand(0)->getType();
5305  llvm::Constant *acf =
5306 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
5307  m->module->getOrInsertFunction(funcName, LLVMTypes::MaskType,
5308  cmpOpType, cmpOpType,
5309  LLVMTypes::MaskType, NULL);
5310 #else // LLVM 5.0+
5311  m->module->getOrInsertFunction(funcName, LLVMTypes::MaskType,
5312  cmpOpType, cmpOpType,
5313  LLVMTypes::MaskType);
5314 #endif
5315  andCmpFunc = llvm::dyn_cast<llvm::Function>(acf);
5316  Assert(andCmpFunc != NULL);
5317  andCmpFunc->setDoesNotThrow();
5318  andCmpFunc->setDoesNotAccessMemory();
5319  }
5320 
5321  // Set up the function call to the *_and_mask function; the
5322  // mask value passed in is the other operand to the AND.
5323  llvm::Value *args[3] = { opCmp->getOperand(0), opCmp->getOperand(1),
5324  bop->getOperand(i ^ 1) };
5325  llvm::ArrayRef<llvm::Value *> argArray(&args[0], &args[3]);
5326  llvm::Instruction *cmpCall =
5327  llvm::CallInst::Create(andCmpFunc, argArray,
5328  LLVMGetName(bop, "_and_mask"),
5329  (llvm::Instruction *)NULL);
5330 
5331  // And replace the original AND instruction with it.
5332  llvm::ReplaceInstWithInst(&*iter, cmpCall);
5333 
5334  modifiedAny = true;
5335  goto restart;
5336  }
5337  }
5338 
5339  return modifiedAny;
5340 }
5341 
5342 ///////////////////////////////////////////////////////////////////////////
5343 // MaskOpsCleanupPass
5344 
5345 /** This pass does various peephole improvements to mask modification
5346  operations. In particular, it converts mask XORs with "all true" to
5347  calls to __not() and replaces operations like and(not(a), b) to
5348  __and_not1(a, b) (and similarly if the second operand has not applied
5349  to it...)
5350  */
5351 class MaskOpsCleanupPass : public llvm::BasicBlockPass {
5352 public:
5353  MaskOpsCleanupPass(llvm::Module *m)
5354  : BasicBlockPass(ID) {
5355  llvm::Type *mt = LLVMTypes::MaskType;
5356 
5357  // Declare the __not, __and_not1, and __and_not2 functions that we
5358  // expect the target to end up providing.
5359  notFunc =
5360 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
5361  llvm::dyn_cast<llvm::Function>(m->getOrInsertFunction("__not", mt, mt, NULL));
5362 #else // LLVM 5.0+
5363  llvm::dyn_cast<llvm::Function>(m->getOrInsertFunction("__not", mt, mt));
5364 #endif
5365  assert(notFunc != NULL);
5366 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
5367  notFunc->addFnAttr(llvm::Attributes::NoUnwind);
5368  notFunc->addFnAttr(llvm::Attributes::ReadNone);
5369 #else /* LLVM 3.3+ */
5370  notFunc->addFnAttr(llvm::Attribute::NoUnwind);
5371  notFunc->addFnAttr(llvm::Attribute::ReadNone);
5372 #endif
5373 
5374  andNotFuncs[0] =
5375 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
5376  llvm::dyn_cast<llvm::Function>(m->getOrInsertFunction("__and_not1", mt, mt, mt,
5377  NULL));
5378 #else // LLVM 5.0+
5379  llvm::dyn_cast<llvm::Function>(m->getOrInsertFunction("__and_not1", mt, mt, mt));
5380 #endif
5381  assert(andNotFuncs[0] != NULL);
5382 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
5383  andNotFuncs[0]->addFnAttr(llvm::Attributes::NoUnwind);
5384  andNotFuncs[0]->addFnAttr(llvm::Attributes::ReadNone);
5385 #else /* LLVM 3.3+ */
5386  andNotFuncs[0]->addFnAttr(llvm::Attribute::NoUnwind);
5387  andNotFuncs[0]->addFnAttr(llvm::Attribute::ReadNone);
5388 #endif
5389  andNotFuncs[1] =
5390 #if ISPC_LLVM_VERSION <= ISPC_LLVM_4_0
5391  llvm::dyn_cast<llvm::Function>(m->getOrInsertFunction("__and_not2", mt, mt, mt,
5392  NULL));
5393 #else // LLVM 5.0+
5394  llvm::dyn_cast<llvm::Function>(m->getOrInsertFunction("__and_not2", mt, mt, mt));
5395 #endif
5396  assert(andNotFuncs[1] != NULL);
5397 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
5398  andNotFuncs[1]->addFnAttr(llvm::Attributes::NoUnwind);
5399  andNotFuncs[1]->addFnAttr(llvm::Attributes::ReadNone);
5400 #else /* LLVM 3.3+ */
5401  andNotFuncs[1]->addFnAttr(llvm::Attribute::NoUnwind);
5402  andNotFuncs[1]->addFnAttr(llvm::Attribute::ReadNone);
5403 #endif
5404  }
5405 
5406 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9 // <= 3.9
5407  const char *getPassName() const { return "MaskOps Cleanup Pass"; }
5408 #else // LLVM 4.0+
5409  llvm::StringRef getPassName() const { return "MaskOps Cleanup Pass"; }
5410 #endif
5411  bool runOnBasicBlock(llvm::BasicBlock &BB);
5412 
5413 private:
5414  llvm::Value *lGetNotOperand(llvm::Value *v) const;
5415 
5416  llvm::Function *notFunc, *andNotFuncs[2];
5417 
5418  static char ID;
5419 };
5420 
5421 char MaskOpsCleanupPass::ID = 0;
5422 
5423 
5424 /** Returns true if the given value is a compile-time constant vector of
5425  i1s with all elements 'true'.
5426 */
5427 static bool
5428 lIsAllTrue(llvm::Value *v) {
5429  if (llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v)) {
5430  llvm::ConstantInt *ci;
5431  return (cv->getSplatValue() != NULL &&
5432  (ci = llvm::dyn_cast<llvm::ConstantInt>(cv->getSplatValue())) != NULL &&
5433  ci->isOne());
5434  }
5435 
5436  if (llvm::ConstantDataVector *cdv = llvm::dyn_cast<llvm::ConstantDataVector>(v)) {
5437  llvm::ConstantInt *ci;
5438  return (cdv->getSplatValue() != NULL &&
5439  (ci = llvm::dyn_cast<llvm::ConstantInt>(cdv->getSplatValue())) != NULL &&
5440  ci->isOne());
5441  }
5442 
5443  return false;
5444 }
5445 
5446 
5447 /** Checks to see if the given value is the NOT of some other value. If
5448  so, it returns the operand of the NOT; otherwise returns NULL.
5449  */
5450 llvm::Value *
5451  MaskOpsCleanupPass::lGetNotOperand(llvm::Value *v) const {
5452  if (llvm::CallInst *ci = llvm::dyn_cast<llvm::CallInst>(v))
5453  if (ci->getCalledFunction() == notFunc)
5454  // Direct call to __not()
5455  return ci->getArgOperand(0);
5456 
5457  if (llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(v))
5458  if (bop->getOpcode() == llvm::Instruction::Xor &&
5459  lIsAllTrue(bop->getOperand(1)))
5460  // XOR of all-true vector.
5461  return bop->getOperand(0);
5462 
5463  return NULL;
5464 }
5465 
5466 
5467 bool
5468 MaskOpsCleanupPass::runOnBasicBlock(llvm::BasicBlock &bb) {
5469  bool modifiedAny = false;
5470 
5471  restart:
5472  for (llvm::BasicBlock::iterator iter = bb.begin(), e = bb.end(); iter != e; ++iter) {
5473  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(&*iter);
5474  if (bop == NULL)
5475  continue;
5476 
5477  if (bop->getType() != LLVMTypes::MaskType)
5478  continue;
5479 
5480  if (bop->getOpcode() == llvm::Instruction::Xor) {
5481  // Check for XOR with all-true values
5482  if (lIsAllTrue(bop->getOperand(1))) {
5483  llvm::Value *val = bop->getOperand(0);
5484  // Note that ArrayRef takes reference to an object, which must live
5485  // long enough, so passing return value of getOperand directly is
5486  // incorrect and it actually causes crashes with gcc 4.7 and later.
5487  llvm::ArrayRef<llvm::Value *> arg(val);
5488  llvm::CallInst *notCall = llvm::CallInst::Create(notFunc, arg,
5489  bop->getName());
5490  ReplaceInstWithInst(&*iter, notCall);
5491  modifiedAny = true;
5492  goto restart;
5493  }
5494  }
5495  else if (bop->getOpcode() == llvm::Instruction::And) {
5496  // Check each of the operands to see if they have NOT applied
5497  // to them.
5498  for (int i = 0; i < 2; ++i) {
5499  if (llvm::Value *notOp = lGetNotOperand(bop->getOperand(i))) {
5500  // In notOp we have the target of the NOT operation;
5501  // put it in its appropriate spot in the operand array.
5502  // Copy in the other operand directly.
5503  llvm::Value *args[2];
5504  args[i] = notOp;
5505  args[i ^ 1] = bop->getOperand(i ^ 1);
5506  llvm::ArrayRef<llvm::Value *> argsRef(&args[0], 2);
5507 
5508  // Call the appropriate __and_not* function.
5509  llvm::CallInst *andNotCall =
5510  llvm::CallInst::Create(andNotFuncs[i], argsRef, bop->getName());
5511 
5512  ReplaceInstWithInst(&*iter, andNotCall);
5513  modifiedAny = true;
5514  goto restart;
5515  }
5516  }
5517  }
5518  }
5519 
5520  return modifiedAny;
5521 }
5522 
5523 
5524 //===----------------------------------------------------------------------===//
5525 // External Interface declaration
5526 //===----------------------------------------------------------------------===//
5527 
5528 bool
5529 WriteCXXFile(llvm::Module *module, const char *fn, int vectorWidth,
5530  const char *includeName) {
5531 
5532 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6 // 3.2, 3.3, 3.4, 3.5, 3.6
5533  llvm::PassManager pm;
5534 #else // LLVM 3.7+
5535  llvm::legacy::PassManager pm;
5536 #endif
5537 #if 0
5538  if (const llvm::TargetData *td = targetMachine->getTargetData())
5539  pm.add(new llvm::TargetData(*td));
5540  else
5541  pm.add(new llvm::TargetData(module));
5542 #endif
5543 
5544 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_3 // 3.2, 3.3
5545  int flags = 0;
5546 #else // LLVM 3.4+
5547  llvm::sys::fs::OpenFlags flags = llvm::sys::fs::F_None;
5548 #endif
5549 
5550 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_5 // 3.2, 3.3, 3.4, 3.5
5551  std::string error;
5552 #else // LLVM 3.6+
5553  std::error_code error;
5554 #endif
5555 
5556 #if ISPC_LLVM_VERSION <= ISPC_LLVM_5_0
5557  llvm::tool_output_file *of = new llvm::tool_output_file(fn, error, flags);
5558 #else // LLVM 6.0+
5559  llvm::ToolOutputFile *of = new llvm::ToolOutputFile(fn, error, flags);
5560 #endif
5561 
5562 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_5 // 3.2, 3.3, 3.4, 3.5
5563  if (error.size()) {
5564 #else // LLVM 3.6+
5565  if (error) {
5566 #endif
5567  fprintf(stderr, "Error opening output file \"%s\".\n", fn);
5568  return false;
5569  }
5570 
5571  llvm::formatted_raw_ostream fos(of->os());
5572 
5573  pm.add(llvm::createGCLoweringPass());
5574  pm.add(llvm::createLowerInvokePass());
5575  pm.add(llvm::createCFGSimplificationPass()); // clean up after lower invoke.
5576  pm.add(new SmearCleanupPass(module, vectorWidth));
5577  pm.add(new AndCmpCleanupPass());
5578  pm.add(new MaskOpsCleanupPass(module));
5579  pm.add(llvm::createDeadCodeEliminationPass()); // clean up after smear pass
5580 //CO pm.add(llvm::createPrintModulePass(&fos));
5581  pm.add(new CWriter(fos, includeName, vectorWidth));
5582 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
5583  // This interface is depricated for 3.3+
5584  pm.add(llvm::createGCInfoDeleter());
5585 #endif
5586 //CO pm.add(llvm::createVerifierPass());
5587 
5588  pm.run(*module);
5589 
5590  return true;
5591 }
static const char * lPredicateToString(llvm::CmpInst::Predicate p)
Definition: cbackend.cpp:3801
Opt opt
Definition: ispc.h:548
Definition: ispc.h:75
bool operator!=(const constant_iterator &x) const
Definition: cbackend.cpp:171
static bool isFPIntBitCast(const llvm::Instruction &I)
Definition: cbackend.cpp:3338
Module * m
Definition: ispc.cpp:101
Target * target
Definition: ispc.h:550
constant_iterator constant_begin(const llvm::Function *F)
Definition: cbackend.cpp:195
static SpecialGlobalClass getGlobalVariableClass(const llvm::GlobalVariable *GV)
Definition: cbackend.cpp:2381
#define Assert(expr)
Definition: ispc.h:173
static void PrintEscapedString(const char *Str, unsigned Length, llvm::raw_ostream &Out)
Definition: cbackend.cpp:2406
bool forceAlignedMemory
Definition: ispc.h:472
static void findUsedArrayAndLongIntTypes(const llvm::Module *m, std::vector< llvm::ArrayType * > &t, std::vector< llvm::IntegerType * > &i, std::vector< bool > &IsVolatile, std::vector< int > &Alignment)
Definition: cbackend.cpp:389
static const char * lGetTypedFunc(const char *base, llvm::Type *matchType, int width)
Definition: cbackend.cpp:1399
static bool is_vec16_i64_ty(llvm::Type *Ty)
Definition: cbackend.cpp:396
Globals * g
Definition: ispc.cpp:100
bool LLVMVectorValuesAllEqual(llvm::Value *v, llvm::Value **splat=NULL)
Definition: llvmutil.cpp:1233
void Error(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:385
llvm::const_inst_iterator InstI
Definition: cbackend.cpp:147
static llvm::Type * Int8Type
Definition: llvmutil.h:76
bool mangleFunctionsWithTarget
Definition: ispc.h:628
Header file with declarations for various LLVM utility stuff.
constant_iterator constant_end(const llvm::Function *F)
Definition: cbackend.cpp:199
ISA getISA() const
Definition: ispc.h:270
bool fastMath
Definition: ispc.h:442
static const char * getFloatBitCastField(llvm::Type *Ty)
Definition: cbackend.cpp:3962
SpecialGlobalClass
Definition: cbackend.cpp:2373
bool operator==(const constant_iterator &x) const
Definition: cbackend.cpp:168
constant_iterator(const llvm::Function *F, bool)
Definition: cbackend.cpp:164
std::string getTreatGenericAsSmth() const
Definition: ispc.h:272
static void generateCompilerSpecificCode(llvm::formatted_raw_ostream &Out, const llvm::DataLayout *TD)
Definition: cbackend.cpp:2235
static bool isFPCSafeToPrint(const llvm::ConstantFP *CFP)
Definition: cbackend.cpp:1245
static std::string CBEMangle(const std::string &S)
Definition: cbackend.cpp:762
static void printLimitValue(llvm::IntegerType &Ty, bool isSigned, bool isMax, llvm::raw_ostream &Out)
Definition: cbackend.cpp:4043
static const char * lTypeToSuffix(llvm::Type *t)
Definition: cbackend.cpp:3835
static void FindStaticTors(llvm::GlobalVariable *GV, std::set< llvm::Function * > &StaticTors)
Definition: cbackend.cpp:2354
static std::string ftostr(const llvm::APFloat &V)
Definition: cbackend.cpp:1215
Declaration of the Module class, which is the ispc-side representation of the results of compiling a ...
constant_iterator & operator++()
Definition: cbackend.cpp:178
Main ispc.header file. Defines Target, Globals and Opt classes.
constant_iterator(const llvm::Function *F)
Definition: cbackend.cpp:157
static bool isSupportedIntegerSize(llvm::IntegerType &T)
Definition: cbackend.cpp:4069