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