summaryrefslogtreecommitdiff
path: root/tools/lto-bugpoint/LTOBugPoint.cpp
blob: a855a0bcda6f8c999d218c585267987feeacb041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//===- LTOBugPoint.cpp - Top-Level LTO BugPoint class ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class contains all of the shared state and information that is used by
// the LTO BugPoint tool to track down bit code files that cause errors.
//
//===----------------------------------------------------------------------===//

#include "LTOBugPoint.h"
#include "llvm/PassManager.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/CodeGen/FileWriters.h"
#include "llvm/Target/SubtargetFeature.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Target/TargetMachineRegistry.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/System/Path.h"
#include "llvm/Config/config.h"
#include <fstream>
#include <iostream>

using namespace llvm;
using namespace Reloc;
/// LTOBugPoint -- Constructor. Popuate list of linker options and
/// list of linker input files.
LTOBugPoint::LTOBugPoint(std::istream &args, std::istream &ins) {

  // Read linker options. Order is important here.
  std::string option;
  while (getline(args, option))
    LinkerOptions.push_back(option);
  
  // Read linker input files. Order is important here.
  std::string inFile;
  while(getline(ins, inFile))
    LinkerInputFiles.push_back(inFile);
}

/// findTroubleMakers - Find minimum set of input files that causes error
/// identified by the script.
bool
LTOBugPoint::findTroubleMakers(SmallVector<std::string, 4> &TroubleMakers,
			       std::string &Script) {

  // First, build native object files set.
  bool bitcodeFileSeen = false;
  unsigned Size = LinkerInputFiles.size();
  for (unsigned I = 0; I < Size; ++I) {
    std::string &FileName = LinkerInputFiles[I];
    sys::Path InputFile(FileName.c_str());
    if (InputFile.isDynamicLibrary() || InputFile.isArchive()) {
      ErrMsg = "Unable to handle input file ";
      ErrMsg += FileName;
      return false;
    }
    else if (InputFile.isBitcodeFile()) {
      bitcodeFileSeen = true;
      if (getNativeObjectFile(FileName) == false)
	return false;
    }
    else
      NativeInputFiles.push_back(FileName);
  }

  if (!bitcodeFileSeen) {
    ErrMsg = "Unable to help!";
    ErrMsg += " Need at least one input file that contains llvm bitcode";
    return false;
  }

  return true;
}

/// getFeatureString - Return a string listing the features associated with the
/// target triple.
///
/// FIXME: This is an inelegant way of specifying the features of a
/// subtarget. It would be better if we could encode this information into the
/// IR.
std::string LTOBugPoint::getFeatureString(const char *TargetTriple) {
  SubtargetFeatures Features;

  if (strncmp(TargetTriple, "powerpc-apple-", 14) == 0) {
    Features.AddFeature("altivec", true);
  } else if (strncmp(TargetTriple, "powerpc64-apple-", 16) == 0) {
    Features.AddFeature("64bit", true);
    Features.AddFeature("altivec", true);
  }

  return Features.getString();
}

/// assembleBitcode - Generate assembly code from the module. Return false
/// in case of an error.
bool LTOBugPoint::assembleBitcode(llvm::Module *M, const char *AsmFileName) {
  std::string TargetTriple = M->getTargetTriple();
  std::string FeatureStr =
    getFeatureString(TargetTriple.c_str());

  const TargetMachineRegistry::entry* Registry =
    TargetMachineRegistry::getClosestStaticTargetForModule(
                                                       *M, ErrMsg);
  if ( Registry == NULL )
    return false;

  TargetMachine *Target = Registry->CtorFn(*M, FeatureStr.c_str());

  // If target supports exception handling then enable it now.
  if (Target->getTargetAsmInfo()->doesSupportExceptionHandling())
    ExceptionHandling = true;
  
  // FIXME
  Target->setRelocationModel(Reloc::PIC_);

  FunctionPassManager* CGPasses =
    new FunctionPassManager(new ExistingModuleProvider(M));
  
  CGPasses->add(new TargetData(*Target->getTargetData()));
  MachineCodeEmitter* mce = NULL;

  std::ofstream *Out = new std::ofstream(AsmFileName, std::ios::out);

  switch (Target->addPassesToEmitFile(*CGPasses, *Out,
				       TargetMachine::AssemblyFile, true)) {
  case FileModel::MachOFile:
    mce = AddMachOWriter(*CGPasses, *Out, *Target);
    break;
  case FileModel::ElfFile:
    mce = AddELFWriter(*CGPasses, *Out, *Target);
    break;
  case FileModel::AsmFile:
    break;
  case FileModel::Error:
  case FileModel::None:
    ErrMsg = "target file type not supported";
    return false;
  }
  
  if (Target->addPassesToEmitFileFinish(*CGPasses, mce, true)) {
    ErrMsg = "target does not support generation of this file type";
    return false;
  }

  CGPasses->doInitialization();
  for (Module::iterator
	 it = M->begin(), e = M->end(); it != e; ++it)
    if (!it->isDeclaration())
      CGPasses->run(*it);
  CGPasses->doFinalization();
  delete Out;
  return true;
}

/// getNativeObjectFile - Generate native object file based from llvm
/// bitcode file. Return false in case of an error.
bool LTOBugPoint::getNativeObjectFile(std::string &FileName) {

  std::auto_ptr<Module> M;
  MemoryBuffer *Buffer
    = MemoryBuffer::getFile(FileName.c_str(), &ErrMsg);
  if (!Buffer) {
    ErrMsg = "Unable to read ";
    ErrMsg += FileName;
    return false;
  }
  M.reset(ParseBitcodeFile(Buffer, &ErrMsg));
  std::string TargetTriple = M->getTargetTriple();

  sys::Path AsmFile(sys::Path::GetTemporaryDirectory());
  if(AsmFile.createTemporaryFileOnDisk(false, &ErrMsg))
    return false;

  if (assembleBitcode(M.get(), AsmFile.c_str()) == false)
    return false;

  sys::Path NativeFile(sys::Path::GetTemporaryDirectory());
  if(NativeFile.createTemporaryFileOnDisk(false, &ErrMsg))
    return false;

  // find compiler driver
  const sys::Path gcc = sys::Program::FindProgramByName("gcc");
  if ( gcc.isEmpty() ) {
    ErrMsg = "can't locate gcc";
    return false;
  }

  // build argument list
  std::vector<const char*> args;
  args.push_back(gcc.c_str());
  if ( TargetTriple.find("darwin") != TargetTriple.size() ) {
    if (strncmp(TargetTriple.c_str(), "i686-apple-", 11) == 0) {
      args.push_back("-arch");
      args.push_back("i386");
    }
    else if (strncmp(TargetTriple.c_str(), "x86_64-apple-", 13) == 0) {
      args.push_back("-arch");
      args.push_back("x86_64");
    }
    else if (strncmp(TargetTriple.c_str(), "powerpc-apple-", 14) == 0) {
      args.push_back("-arch");
      args.push_back("ppc");
    }
    else if (strncmp(TargetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
      args.push_back("-arch");
      args.push_back("ppc64");
    }
  }
  args.push_back("-c");
  args.push_back("-x");
  args.push_back("assembler");
  args.push_back("-o");
  args.push_back(NativeFile.c_str());
  args.push_back(AsmFile.c_str());
  args.push_back(0);
  
  // invoke assembler
  if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 0, 0, &ErrMsg)) {
    ErrMsg = "error in assembly";    
    return false;
  }
  return true;
}