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