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