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