1249259Sdim//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10263509Sdim// This file implements the auto-upgrade helper functions
11249259Sdim//
12249259Sdim//===----------------------------------------------------------------------===//
13249259Sdim
14249259Sdim#include "llvm/AutoUpgrade.h"
15263509Sdim#include "llvm/DebugInfo.h"
16249259Sdim#include "llvm/IR/Constants.h"
17249259Sdim#include "llvm/IR/Function.h"
18249259Sdim#include "llvm/IR/IRBuilder.h"
19249259Sdim#include "llvm/IR/Instruction.h"
20249259Sdim#include "llvm/IR/IntrinsicInst.h"
21249259Sdim#include "llvm/IR/LLVMContext.h"
22249259Sdim#include "llvm/IR/Module.h"
23249259Sdim#include "llvm/Support/CFG.h"
24249259Sdim#include "llvm/Support/CallSite.h"
25249259Sdim#include "llvm/Support/ErrorHandling.h"
26249259Sdim#include <cstring>
27249259Sdimusing namespace llvm;
28249259Sdim
29249259Sdim// Upgrade the declarations of the SSE4.1 functions whose arguments have
30249259Sdim// changed their type from v4f32 to v2i64.
31249259Sdimstatic bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
32249259Sdim                                 Function *&NewFn) {
33249259Sdim  // Check whether this is an old version of the function, which received
34249259Sdim  // v4f32 arguments.
35249259Sdim  Type *Arg0Type = F->getFunctionType()->getParamType(0);
36249259Sdim  if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
37249259Sdim    return false;
38249259Sdim
39249259Sdim  // Yes, it's old, replace it with new version.
40249259Sdim  F->setName(F->getName() + ".old");
41249259Sdim  NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
42249259Sdim  return true;
43249259Sdim}
44249259Sdim
45249259Sdimstatic bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
46249259Sdim  assert(F && "Illegal to upgrade a non-existent Function.");
47249259Sdim
48249259Sdim  // Quickly eliminate it, if it's not a candidate.
49249259Sdim  StringRef Name = F->getName();
50249259Sdim  if (Name.size() <= 8 || !Name.startswith("llvm."))
51249259Sdim    return false;
52249259Sdim  Name = Name.substr(5); // Strip off "llvm."
53249259Sdim
54249259Sdim  switch (Name[0]) {
55249259Sdim  default: break;
56249259Sdim  case 'a': {
57249259Sdim    if (Name.startswith("arm.neon.vclz")) {
58249259Sdim      Type* args[2] = {
59263509Sdim        F->arg_begin()->getType(),
60249259Sdim        Type::getInt1Ty(F->getContext())
61249259Sdim      };
62249259Sdim      // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
63249259Sdim      // the end of the name. Change name from llvm.arm.neon.vclz.* to
64249259Sdim      //  llvm.ctlz.*
65249259Sdim      FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
66263509Sdim      NewFn = Function::Create(fType, F->getLinkage(),
67249259Sdim                               "llvm.ctlz." + Name.substr(14), F->getParent());
68249259Sdim      return true;
69249259Sdim    }
70249259Sdim    if (Name.startswith("arm.neon.vcnt")) {
71249259Sdim      NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
72249259Sdim                                        F->arg_begin()->getType());
73249259Sdim      return true;
74249259Sdim    }
75249259Sdim    break;
76249259Sdim  }
77249259Sdim  case 'c': {
78249259Sdim    if (Name.startswith("ctlz.") && F->arg_size() == 1) {
79249259Sdim      F->setName(Name + ".old");
80249259Sdim      NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
81249259Sdim                                        F->arg_begin()->getType());
82249259Sdim      return true;
83249259Sdim    }
84249259Sdim    if (Name.startswith("cttz.") && F->arg_size() == 1) {
85249259Sdim      F->setName(Name + ".old");
86249259Sdim      NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
87249259Sdim                                        F->arg_begin()->getType());
88249259Sdim      return true;
89249259Sdim    }
90249259Sdim    break;
91249259Sdim  }
92263509Sdim  case 'o':
93263509Sdim    // We only need to change the name to match the mangling including the
94263509Sdim    // address space.
95263509Sdim    if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
96263509Sdim      Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
97263509Sdim      if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
98263509Sdim        F->setName(Name + ".old");
99263509Sdim        NewFn = Intrinsic::getDeclaration(F->getParent(),
100263509Sdim                                          Intrinsic::objectsize, Tys);
101263509Sdim        return true;
102263509Sdim      }
103263509Sdim    }
104263509Sdim    break;
105263509Sdim
106249259Sdim  case 'x': {
107249259Sdim    if (Name.startswith("x86.sse2.pcmpeq.") ||
108249259Sdim        Name.startswith("x86.sse2.pcmpgt.") ||
109249259Sdim        Name.startswith("x86.avx2.pcmpeq.") ||
110249259Sdim        Name.startswith("x86.avx2.pcmpgt.") ||
111249259Sdim        Name.startswith("x86.avx.vpermil.") ||
112249259Sdim        Name == "x86.avx.movnt.dq.256" ||
113249259Sdim        Name == "x86.avx.movnt.pd.256" ||
114249259Sdim        Name == "x86.avx.movnt.ps.256" ||
115263509Sdim        Name == "x86.sse42.crc32.64.8" ||
116249259Sdim        (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
117249259Sdim      NewFn = 0;
118249259Sdim      return true;
119249259Sdim    }
120249259Sdim    // SSE4.1 ptest functions may have an old signature.
121249259Sdim    if (Name.startswith("x86.sse41.ptest")) {
122249259Sdim      if (Name == "x86.sse41.ptestc")
123249259Sdim        return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
124249259Sdim      if (Name == "x86.sse41.ptestz")
125249259Sdim        return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
126249259Sdim      if (Name == "x86.sse41.ptestnzc")
127249259Sdim        return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
128249259Sdim    }
129249259Sdim    // frcz.ss/sd may need to have an argument dropped
130249259Sdim    if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
131249259Sdim      F->setName(Name + ".old");
132249259Sdim      NewFn = Intrinsic::getDeclaration(F->getParent(),
133249259Sdim                                        Intrinsic::x86_xop_vfrcz_ss);
134249259Sdim      return true;
135249259Sdim    }
136249259Sdim    if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
137249259Sdim      F->setName(Name + ".old");
138249259Sdim      NewFn = Intrinsic::getDeclaration(F->getParent(),
139249259Sdim                                        Intrinsic::x86_xop_vfrcz_sd);
140249259Sdim      return true;
141249259Sdim    }
142249259Sdim    // Fix the FMA4 intrinsics to remove the 4
143249259Sdim    if (Name.startswith("x86.fma4.")) {
144249259Sdim      F->setName("llvm.x86.fma" + Name.substr(8));
145249259Sdim      NewFn = F;
146249259Sdim      return true;
147249259Sdim    }
148249259Sdim    break;
149249259Sdim  }
150249259Sdim  }
151249259Sdim
152249259Sdim  //  This may not belong here. This function is effectively being overloaded
153249259Sdim  //  to both detect an intrinsic which needs upgrading, and to provide the
154249259Sdim  //  upgraded form of the intrinsic. We should perhaps have two separate
155249259Sdim  //  functions for this.
156249259Sdim  return false;
157249259Sdim}
158249259Sdim
159249259Sdimbool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
160249259Sdim  NewFn = 0;
161249259Sdim  bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
162249259Sdim
163249259Sdim  // Upgrade intrinsic attributes.  This does not change the function.
164249259Sdim  if (NewFn)
165249259Sdim    F = NewFn;
166249259Sdim  if (unsigned id = F->getIntrinsicID())
167249259Sdim    F->setAttributes(Intrinsic::getAttributes(F->getContext(),
168249259Sdim                                              (Intrinsic::ID)id));
169249259Sdim  return Upgraded;
170249259Sdim}
171249259Sdim
172249259Sdimbool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
173249259Sdim  // Nothing to do yet.
174249259Sdim  return false;
175249259Sdim}
176249259Sdim
177249259Sdim// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
178249259Sdim// upgraded intrinsic. All argument and return casting must be provided in
179249259Sdim// order to seamlessly integrate with existing context.
180249259Sdimvoid llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
181249259Sdim  Function *F = CI->getCalledFunction();
182249259Sdim  LLVMContext &C = CI->getContext();
183249259Sdim  IRBuilder<> Builder(C);
184249259Sdim  Builder.SetInsertPoint(CI->getParent(), CI);
185249259Sdim
186249259Sdim  assert(F && "Intrinsic call is not direct?");
187249259Sdim
188249259Sdim  if (!NewFn) {
189249259Sdim    // Get the Function's name.
190249259Sdim    StringRef Name = F->getName();
191249259Sdim
192249259Sdim    Value *Rep;
193249259Sdim    // Upgrade packed integer vector compares intrinsics to compare instructions
194249259Sdim    if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
195249259Sdim        Name.startswith("llvm.x86.avx2.pcmpeq.")) {
196249259Sdim      Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
197249259Sdim                                 "pcmpeq");
198249259Sdim      // need to sign extend since icmp returns vector of i1
199249259Sdim      Rep = Builder.CreateSExt(Rep, CI->getType(), "");
200249259Sdim    } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
201249259Sdim               Name.startswith("llvm.x86.avx2.pcmpgt.")) {
202249259Sdim      Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
203249259Sdim                                  "pcmpgt");
204249259Sdim      // need to sign extend since icmp returns vector of i1
205249259Sdim      Rep = Builder.CreateSExt(Rep, CI->getType(), "");
206249259Sdim    } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
207249259Sdim               Name == "llvm.x86.avx.movnt.ps.256" ||
208249259Sdim               Name == "llvm.x86.avx.movnt.pd.256") {
209249259Sdim      IRBuilder<> Builder(C);
210249259Sdim      Builder.SetInsertPoint(CI->getParent(), CI);
211249259Sdim
212249259Sdim      Module *M = F->getParent();
213249259Sdim      SmallVector<Value *, 1> Elts;
214249259Sdim      Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
215249259Sdim      MDNode *Node = MDNode::get(C, Elts);
216249259Sdim
217249259Sdim      Value *Arg0 = CI->getArgOperand(0);
218249259Sdim      Value *Arg1 = CI->getArgOperand(1);
219249259Sdim
220249259Sdim      // Convert the type of the pointer to a pointer to the stored type.
221249259Sdim      Value *BC = Builder.CreateBitCast(Arg0,
222249259Sdim                                        PointerType::getUnqual(Arg1->getType()),
223249259Sdim                                        "cast");
224249259Sdim      StoreInst *SI = Builder.CreateStore(Arg1, BC);
225249259Sdim      SI->setMetadata(M->getMDKindID("nontemporal"), Node);
226249259Sdim      SI->setAlignment(16);
227249259Sdim
228249259Sdim      // Remove intrinsic.
229249259Sdim      CI->eraseFromParent();
230249259Sdim      return;
231249259Sdim    } else if (Name.startswith("llvm.x86.xop.vpcom")) {
232249259Sdim      Intrinsic::ID intID;
233249259Sdim      if (Name.endswith("ub"))
234249259Sdim        intID = Intrinsic::x86_xop_vpcomub;
235249259Sdim      else if (Name.endswith("uw"))
236249259Sdim        intID = Intrinsic::x86_xop_vpcomuw;
237249259Sdim      else if (Name.endswith("ud"))
238249259Sdim        intID = Intrinsic::x86_xop_vpcomud;
239249259Sdim      else if (Name.endswith("uq"))
240249259Sdim        intID = Intrinsic::x86_xop_vpcomuq;
241249259Sdim      else if (Name.endswith("b"))
242249259Sdim        intID = Intrinsic::x86_xop_vpcomb;
243249259Sdim      else if (Name.endswith("w"))
244249259Sdim        intID = Intrinsic::x86_xop_vpcomw;
245249259Sdim      else if (Name.endswith("d"))
246249259Sdim        intID = Intrinsic::x86_xop_vpcomd;
247249259Sdim      else if (Name.endswith("q"))
248249259Sdim        intID = Intrinsic::x86_xop_vpcomq;
249249259Sdim      else
250249259Sdim        llvm_unreachable("Unknown suffix");
251249259Sdim
252249259Sdim      Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
253249259Sdim      unsigned Imm;
254249259Sdim      if (Name.startswith("lt"))
255249259Sdim        Imm = 0;
256249259Sdim      else if (Name.startswith("le"))
257249259Sdim        Imm = 1;
258249259Sdim      else if (Name.startswith("gt"))
259249259Sdim        Imm = 2;
260249259Sdim      else if (Name.startswith("ge"))
261249259Sdim        Imm = 3;
262249259Sdim      else if (Name.startswith("eq"))
263249259Sdim        Imm = 4;
264249259Sdim      else if (Name.startswith("ne"))
265249259Sdim        Imm = 5;
266249259Sdim      else if (Name.startswith("true"))
267249259Sdim        Imm = 6;
268249259Sdim      else if (Name.startswith("false"))
269249259Sdim        Imm = 7;
270249259Sdim      else
271249259Sdim        llvm_unreachable("Unknown condition");
272249259Sdim
273249259Sdim      Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
274249259Sdim      Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
275249259Sdim                                CI->getArgOperand(1), Builder.getInt8(Imm));
276263509Sdim    } else if (Name == "llvm.x86.sse42.crc32.64.8") {
277263509Sdim      Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
278263509Sdim                                               Intrinsic::x86_sse42_crc32_32_8);
279263509Sdim      Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
280263509Sdim      Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
281263509Sdim      Rep = Builder.CreateZExt(Rep, CI->getType(), "");
282249259Sdim    } else {
283249259Sdim      bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
284249259Sdim      if (Name == "llvm.x86.avx.vpermil.pd.256")
285249259Sdim        PD256 = true;
286249259Sdim      else if (Name == "llvm.x86.avx.vpermil.pd")
287249259Sdim        PD128 = true;
288249259Sdim      else if (Name == "llvm.x86.avx.vpermil.ps.256")
289249259Sdim        PS256 = true;
290249259Sdim      else if (Name == "llvm.x86.avx.vpermil.ps")
291249259Sdim        PS128 = true;
292249259Sdim
293249259Sdim      if (PD256 || PD128 || PS256 || PS128) {
294249259Sdim        Value *Op0 = CI->getArgOperand(0);
295249259Sdim        unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
296249259Sdim        SmallVector<Constant*, 8> Idxs;
297249259Sdim
298249259Sdim        if (PD128)
299249259Sdim          for (unsigned i = 0; i != 2; ++i)
300249259Sdim            Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
301249259Sdim        else if (PD256)
302249259Sdim          for (unsigned l = 0; l != 4; l+=2)
303249259Sdim            for (unsigned i = 0; i != 2; ++i)
304249259Sdim              Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
305249259Sdim        else if (PS128)
306249259Sdim          for (unsigned i = 0; i != 4; ++i)
307249259Sdim            Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
308249259Sdim        else if (PS256)
309249259Sdim          for (unsigned l = 0; l != 8; l+=4)
310249259Sdim            for (unsigned i = 0; i != 4; ++i)
311249259Sdim              Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
312249259Sdim        else
313249259Sdim          llvm_unreachable("Unexpected function");
314249259Sdim
315249259Sdim        Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
316249259Sdim      } else {
317249259Sdim        llvm_unreachable("Unknown function for CallInst upgrade.");
318249259Sdim      }
319249259Sdim    }
320249259Sdim
321249259Sdim    CI->replaceAllUsesWith(Rep);
322249259Sdim    CI->eraseFromParent();
323249259Sdim    return;
324249259Sdim  }
325249259Sdim
326249259Sdim  std::string Name = CI->getName().str();
327249259Sdim  CI->setName(Name + ".old");
328249259Sdim
329249259Sdim  switch (NewFn->getIntrinsicID()) {
330249259Sdim  default:
331249259Sdim    llvm_unreachable("Unknown function for CallInst upgrade.");
332249259Sdim
333249259Sdim  case Intrinsic::ctlz:
334249259Sdim  case Intrinsic::cttz:
335249259Sdim    assert(CI->getNumArgOperands() == 1 &&
336249259Sdim           "Mismatch between function args and call args");
337249259Sdim    CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
338249259Sdim                                               Builder.getFalse(), Name));
339249259Sdim    CI->eraseFromParent();
340249259Sdim    return;
341249259Sdim
342263509Sdim  case Intrinsic::objectsize:
343263509Sdim    CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
344263509Sdim                                               CI->getArgOperand(0),
345263509Sdim                                               CI->getArgOperand(1),
346263509Sdim                                               Name));
347263509Sdim    CI->eraseFromParent();
348263509Sdim    return;
349263509Sdim
350249259Sdim  case Intrinsic::arm_neon_vclz: {
351249259Sdim    // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
352249259Sdim    CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
353249259Sdim                                               Builder.getFalse(),
354249259Sdim                                               "llvm.ctlz." + Name.substr(14)));
355249259Sdim    CI->eraseFromParent();
356249259Sdim    return;
357249259Sdim  }
358249259Sdim  case Intrinsic::ctpop: {
359249259Sdim    CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
360249259Sdim    CI->eraseFromParent();
361249259Sdim    return;
362249259Sdim  }
363249259Sdim
364249259Sdim  case Intrinsic::x86_xop_vfrcz_ss:
365249259Sdim  case Intrinsic::x86_xop_vfrcz_sd:
366249259Sdim    CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
367249259Sdim                                              Name));
368249259Sdim    CI->eraseFromParent();
369249259Sdim    return;
370249259Sdim
371249259Sdim  case Intrinsic::x86_sse41_ptestc:
372249259Sdim  case Intrinsic::x86_sse41_ptestz:
373249259Sdim  case Intrinsic::x86_sse41_ptestnzc: {
374249259Sdim    // The arguments for these intrinsics used to be v4f32, and changed
375249259Sdim    // to v2i64. This is purely a nop, since those are bitwise intrinsics.
376249259Sdim    // So, the only thing required is a bitcast for both arguments.
377249259Sdim    // First, check the arguments have the old type.
378249259Sdim    Value *Arg0 = CI->getArgOperand(0);
379249259Sdim    if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
380249259Sdim      return;
381249259Sdim
382249259Sdim    // Old intrinsic, add bitcasts
383249259Sdim    Value *Arg1 = CI->getArgOperand(1);
384249259Sdim
385249259Sdim    Value *BC0 =
386249259Sdim      Builder.CreateBitCast(Arg0,
387249259Sdim                            VectorType::get(Type::getInt64Ty(C), 2),
388249259Sdim                            "cast");
389249259Sdim    Value *BC1 =
390249259Sdim      Builder.CreateBitCast(Arg1,
391249259Sdim                            VectorType::get(Type::getInt64Ty(C), 2),
392249259Sdim                            "cast");
393249259Sdim
394249259Sdim    CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
395249259Sdim    CI->replaceAllUsesWith(NewCall);
396249259Sdim    CI->eraseFromParent();
397249259Sdim    return;
398249259Sdim  }
399249259Sdim  }
400249259Sdim}
401249259Sdim
402263509Sdim// This tests each Function to determine if it needs upgrading. When we find
403263509Sdim// one we are interested in, we then upgrade all calls to reflect the new
404249259Sdim// function.
405249259Sdimvoid llvm::UpgradeCallsToIntrinsic(Function* F) {
406249259Sdim  assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
407249259Sdim
408249259Sdim  // Upgrade the function and check if it is a totaly new function.
409249259Sdim  Function *NewFn;
410249259Sdim  if (UpgradeIntrinsicFunction(F, NewFn)) {
411249259Sdim    if (NewFn != F) {
412249259Sdim      // Replace all uses to the old function with the new one if necessary.
413249259Sdim      for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
414249259Sdim           UI != UE; ) {
415249259Sdim        if (CallInst *CI = dyn_cast<CallInst>(*UI++))
416249259Sdim          UpgradeIntrinsicCall(CI, NewFn);
417249259Sdim      }
418249259Sdim      // Remove old function, no longer used, from the module.
419249259Sdim      F->eraseFromParent();
420249259Sdim    }
421249259Sdim  }
422249259Sdim}
423249259Sdim
424263509Sdimvoid llvm::UpgradeInstWithTBAATag(Instruction *I) {
425263509Sdim  MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
426263509Sdim  assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
427263509Sdim  // Check if the tag uses struct-path aware TBAA format.
428263509Sdim  if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
429263509Sdim    return;
430263509Sdim
431263509Sdim  if (MD->getNumOperands() == 3) {
432263509Sdim    Value *Elts[] = {
433263509Sdim      MD->getOperand(0),
434263509Sdim      MD->getOperand(1)
435263509Sdim    };
436263509Sdim    MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
437263509Sdim    // Create a MDNode <ScalarType, ScalarType, offset 0, const>
438263509Sdim    Value *Elts2[] = {
439263509Sdim      ScalarType, ScalarType,
440263509Sdim      Constant::getNullValue(Type::getInt64Ty(I->getContext())),
441263509Sdim      MD->getOperand(2)
442263509Sdim    };
443263509Sdim    I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
444263509Sdim  } else {
445263509Sdim    // Create a MDNode <MD, MD, offset 0>
446263509Sdim    Value *Elts[] = {MD, MD,
447263509Sdim      Constant::getNullValue(Type::getInt64Ty(I->getContext()))};
448263509Sdim    I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
449263509Sdim  }
450263509Sdim}
451263509Sdim
452263509SdimInstruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
453263509Sdim                                      Instruction *&Temp) {
454263509Sdim  if (Opc != Instruction::BitCast)
455263509Sdim    return 0;
456263509Sdim
457263509Sdim  Temp = 0;
458263509Sdim  Type *SrcTy = V->getType();
459263509Sdim  if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
460263509Sdim      SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
461263509Sdim    LLVMContext &Context = V->getContext();
462263509Sdim
463263509Sdim    // We have no information about target data layout, so we assume that
464263509Sdim    // the maximum pointer size is 64bit.
465263509Sdim    Type *MidTy = Type::getInt64Ty(Context);
466263509Sdim    Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
467263509Sdim
468263509Sdim    return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
469263509Sdim  }
470263509Sdim
471263509Sdim  return 0;
472263509Sdim}
473263509Sdim
474263509SdimValue *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
475263509Sdim  if (Opc != Instruction::BitCast)
476263509Sdim    return 0;
477263509Sdim
478263509Sdim  Type *SrcTy = C->getType();
479263509Sdim  if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
480263509Sdim      SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
481263509Sdim    LLVMContext &Context = C->getContext();
482263509Sdim
483263509Sdim    // We have no information about target data layout, so we assume that
484263509Sdim    // the maximum pointer size is 64bit.
485263509Sdim    Type *MidTy = Type::getInt64Ty(Context);
486263509Sdim
487263509Sdim    return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
488263509Sdim                                     DestTy);
489263509Sdim  }
490263509Sdim
491263509Sdim  return 0;
492263509Sdim}
493263509Sdim
494263509Sdim/// Check the debug info version number, if it is out-dated, drop the debug
495263509Sdim/// info. Return true if module is modified.
496263509Sdimbool llvm::UpgradeDebugInfo(Module &M) {
497263509Sdim  if (getDebugMetadataVersionFromModule(M) == DEBUG_METADATA_VERSION)
498263509Sdim    return false;
499263509Sdim
500263509Sdim  return StripDebugInfo(M);
501263509Sdim}
502